简体   繁体   中英

C Code using Glib library building succesfully on windows gives compiler/linker error on linux

I have a code which makes use of Glib library to create a new type. Now the code is as:

.h file:

G_DECLARE_DERIVABLE_TYPE(NewTypeState, new_type_state, NEW, TYPE_STATE, ParentType)

.c file:

G_DEFINE_TYPE_WITH_PRIVATE(NewTypeState, new_type_state, NEW_TYPE_STATE)

now as per the glib doc for object creation we have to define two static functions as:

static new_type_init()

static new_type_class_init()

But in the code instead of defining static new_type_init() the func. is declared as:

__declspec(dllexport) extern void new_type_init()

and defined as:

void new_type_init()
{}

And the code compiles and links successfully on windows but while trying to build on linux I removed the __declspec(dllexport) but I started getting the error:

static declaration follows non-static declaration

Now I commented out the declaration from .h file and only has non-static definition in the .c file but when trying to build it I get the linker error:

undefined reference new_type_init

I even added the extern to definition but the error stays.

Could someone explain the above behavior?

EDIT:

o/p for objdump -t <file-name> | grep -i <symbol-name> objdump -t <file-name> | grep -i <symbol-name> is:

000000000000b440 l     F .text  0000000000000023              symbol-name
0000000000000000         *UND*  0000000000000000              symbol-name

Extern indicates the function will be available later (ie is not defined here in this translation unit). Static forces the function to be unavailable externally. Add extern to the declaration in the header and remove static from the definition and you may have some success. That being said, I do not know Glib so cannot say for sure.

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