C++ undefined reference to defined function

前端 未结 6 946

I cannot figure out why this is not working. I will put up all three of my files and possibly someone can tell me why it is throwing this error. I am using g++ to compile the

6条回答
  •  臣服心动
    2021-02-03 20:51

    Though previous posters covered your particular error, you can get 'Undefined reference' linker errors when attempting to compile C code with g++, if you don't tell the compiler to use C linkage.

    For example you should do this in your C header files:

    extern "C" {
    
    ...
    
    void myfunc(int param);
    
    ...
    
    }
    

    To make 'myfunc' available in C++ programs.

    If you still also want to use this from C, wrap the extern "C" { and } in #ifdef __cplusplus preprocessor conditionals, like

    #ifdef __cplusplus
    extern "C" {
    #endif
    

    This way, the extern block will just be “skipped” when using a C compiler.

提交回复
热议问题