Static Library output with Odin #3784
-
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.
|
Beta Was this translation helpful? Give feedback.
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:
|
Beta Was this translation helpful? Give feedback.
-
You can add |
Beta Was this translation helpful? Give feedback.
-
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 (
|
Beta Was this translation helpful? Give feedback.
Posting the complete solution here.
First, some info:
Here's the C++ file:
Here's the Odin file: