Can't create shared library with static inside

后端 未结 3 645
误落风尘
误落风尘 2020-12-10 21:32

Please help I search throw whole internet but I can\'t find answer ...

C Layer

I have created simple function int mean(int, int); and place i

3条回答
  •  抹茶落季
    2020-12-10 22:00

    Simply use C compiler instead!

    You compile with C++ compiler (Makefile has "$(GPP) -c *.c") and the function symbol name includes parameter information and is called different: not "mean", but something more complex, similar as Java names symbols internally. You can use "nm" to see what's inside an object file.

    For example my g++ generates:

    # nm x.o
    00000000 T _Z4meanii
    

    So there is no "mean" (but "Z4meanii"). Using a C compiler, and the symbol names are straightforward:

    # nm x.o
    00000000 T mean
    

    (Please note that the same source code content was compiled)

    So change in you Makefile:

    obj:
        $(GCC) -c *.c
    

    If for some reason a C++ compiler must be used, then extern "C" must be added:

    calc_mean.h (simplified, without inclusion guards etc.)

    #ifdef __cplusplus
    extern "C" {
    #endif
    
    int mean(int, int);
    
    #ifdef __cplusplus
    } // extern "C"
    #endif
    

    In general, if you need C symbol names in C++ files, use extern "C". Test_Library.h uses it correctly and provides a nice example. You can put the same extern "C" logic (best including if #ifdef __cplusplus block) to your header file. Some consider it good practice to always do so.

    But note that there are several, sometimes subtle, differences between C and C++, so I recommend to compile C sources with C compiler, although I know that there can be advantages using a C++ compiler.

    oki,

    Steffen

提交回复
热议问题