C++ undefined reference to defined function

前端 未结 6 947

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:33

    As Paul said, this can be a linker complaint, rather than a compiler error. If you read your build output/logs carefully (may need to look in a separate IDE window to see the full details) you can dell if the problem is from the compiler (needs to be fixed in code) or from the linker (and need to be fixed in the make/cmake/project level to include a missing lib).

    0 讨论(0)
  • 2021-02-03 20:37

    If you are including a library which depends on another library, then the order of inclusion is also important:

    g++ -o MyApp MyMain.o -lMyLib1 -lMyLib2
    

    In this case, it is okay if MyLib1 depends on MyLib2. However, if there reverse is true, you will get undefined references.

    0 讨论(0)
  • 2021-02-03 20:43

    You need to compile and link all your source files together:

    g++ main.c function_file.c
    
    0 讨论(0)
  • 2021-02-03 20:47

    The declaration and definition of insertLike are different

    In your header file:

    void insertLike(const char sentence[], const int lengthTo, const int length, const char writeTo[]);

    In your 'function file':

    void insertLike(const char sentence[], const int lengthTo, const int length,char writeTo[]);

    C++ allows function overloading, where you can have multiple functions/methods with the same name, as long as they have different arguments. The argument types are part of the function's signature.

    In this case, insertLike which takes const char* as its fourth parameter and insertLike which takes char * as its fourth parameter are different functions.

    0 讨论(0)
  • 2021-02-03 20:50

    This could also happen if you are using CMake. If you have created a new class and you want to instantiate it, at the constructor call you will receive this error -even when the header and the cpp files are correct- if you have not modified CMakeLists.txt accordingly.

    With CMake, every time you create a new class, before using it the header, the cpp files and any other compilable files (like Qt ui files) must be added to CMakeLists.txt and then re-run cmake . where CMakeLists.txt is stored.

    For example, in this CMakeLists.txt file:

    cmake_minimum_required(VERSION 2.8.11)
    
    project(yourProject)
    
    file(GLOB ImageFeatureDetector_SRC *.h *.cpp)
    
    ### Add your new files here ###
    add_executable(yourProject YourNewClass.h YourNewClass.cpp otherNewFile.ui})
    
    target_link_libraries(imagefeaturedetector ${SomeLibs})
    

    If you are using the command file(GLOB yourProject_SRC *.h *.cpp) then you just need to re-run cmake . without modifying CMakeLists.txt.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题