Search code examples
c++cmakeinclude

".cpp" files in project include path would not be seen by the project


Say I have a simple project Test. Here is the CMakeLists.txt:

project(Test)

set(CMAKE_CXX_STANDARD 11)

include_directories(path/to/somewhere)

add_executable(Test main.cpp)

Now, in the include directory I have A.h and A.cpp. Whenever I compile, I always get undefined reference to the definition of the functions declared in A.h. It is clear that the project is not seeing A.cpp at all.

May I please know how do I make it work? Given that I prefer not doing add_executable(Test main.cpp A.cpp) since I think A should rather be a separate thing than integrated into the executable directly. Is my only option building A into a.so? Can I somehow modify the cmake to work around building library?


Solution

  • “.cpp” files in project include path would not be seen by the project

    Your problem is not about what the project "sees". The problem is that you failed to link with the translation unit.

    Is my only option building A into a.so?

    No, you don't need to build a shared library. You can use a static or "object" library instead.

    Or you can list the source file in the add_executable directive.