C++ undefined reference to defined function

前端 未结 6 950

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

提交回复
热议问题