简体   繁体   English

OSX C ++导出功能

[英]OSX C++ Export function

I got a large C++ project that uses a makefile on OSX. 我得到了一个在OSX上使用makefile的大型C ++项目。 where I simply want to add an exported function that I can resolve can call. 我只是想添加一个我可以解析的可以调用的导出函数。 The project needs to be an executeable and not a library. 该项目必须是可执行的,而不是库。 It already exports a bunch of functions but I cannot get my export to work, meaning it's not visible when I run nm on the binary. 它已经导出了很多函数,但是我无法导出工作,这意味着在二进制文件上运行nm时不可见。

I tried to simply add this to the header .h 我试图将其简单地添加到标题.h中

#define EXPORT __attribute__((visibility("default")))
EXPORT int callme(int test);

I also tried declaring it in the header like 我也尝试在标头中声明它

EXPORT int callme(int test) {return 0;}

but then i get a bunch of errors of the symbol already existing in other object files. 但是然后我得到了其他目标文件中已经存在的符号错误的一堆。

Edit: It's a simple C function and I also tried 编辑:这是一个简单的C函数,我也尝试过

 #if !defined(__cplusplus)
 #define MONExternC extern
 #else
 #define MONExternC extern "C"
 #endif

 MONExternC int callme (int test);

I'm a bit of novice when it comes to C++ and any insight or help would be much appreciated, thanks. 对于C ++,我还是个新手,非常感谢您的任何见解或帮助。

You have defined (not just declared) a standalone, non-member, non-template, non-inline function in a header. 您已在标头中定义 (而不只是声明)独立的,非成员,非模板,非内联函数。 This is exactly the thing you should not be doing. 这正是您不应该做的事情。 It leads to the sort of errors you describe, and is prohibited by the standard. 它会导致您描述的某种错误,并且是标准所禁止的。

You should have only a function ptototype in the header. 标头中应仅具有函数原型。 Move its definition to an exactly one source file. 将其定义移到恰好一个源文件中。

The visibility attribute only makes sense when building shared libraries. 可见性属性仅在构建共享库时才有意义。 You probably don't need it. 您可能不需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM