Static Library output with Odin #3784
Answered
by
herohiralal
herohiralal
asked this question in
Q&A
|
I have a C++ project and I'd like to expose an Odin API. For that I would like to link statically. So I have a few different questions, that I'd appreciate if someone can help answer.
|
Answered by
herohiralal
Jun 20, 2024
Replies: 3 comments 1 reply
|
Providing some more info. Directly using Here's the C++ file: // main.cpp
#include <iostream>
extern "C" int UncoolMul(int a, int b)
{
return a * b;
}
extern "C" {
int CoolAdd(int a, int b);
int CoolMul(int a, int b);
}
int main(int argc, char* argv[])
{
std::cout << "Hello, OdinLinkTest!\n";
std::cout << CoolAdd(3, 4);
return 0;
}Here's the Odin file: // main.odin
package main
import "core:fmt"
CoolAdd :: proc "c" (a: i32, b: i32) -> i32 {
return a + b
}
foreign {
UncoolMul :: proc "c" (a: i32, b: i32) -> i32 ---
}
CoolMul :: proc "c" (a: i32, b: i32) -> i32 {
return UncoolMul(a, b)
}Odin code compiled with: Modified Visual C++ Project: Here's the C++ compiler output: |
0 replies
|
You can add |
1 reply
|
Posting the complete solution here. First, some info: Here's the C++ file: // main.cpp
#include <iostream>
extern "C" int UncoolMul(int a, int b)
{
return a * b;
}
extern "C" {
int CoolAdd(int a, int b);
int CoolMul(int a, int b);
}
int main(int argc, char* argv[])
{
std::cout << CoolAdd(3, 4) << '\n';
std::cout << CoolMul(3, 4) << '\n';
return 0;
}Here's the Odin file: // kk.odin
package main
import "core:fmt"
@export
CoolAdd :: proc "c" (a: i32, b: i32) -> i32 {
return a + b
}
foreign {
UncoolMul :: proc "c" (a: i32, b: i32) -> i32 ---
}
@export // CRITICAL - works similar to `#[no_mangle] in rust ffi`
CoolMul :: proc "c" (a: i32, b: i32) -> i32 {
return UncoolMul(a, b)
}Odin code compiled with: Modified Visual C++ Project ( |
0 replies
Answer selected by
herohiralal
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Posting the complete solution here.
First, some info:
Here's the C++ file:
Here's the Odin file: