简体   繁体   中英

OSX C++ Export function

I got a large C++ project that uses a makefile on OSX. 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.

I tried to simply add this to the header .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

 #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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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