My project folder contains src and proto subfolders which hold my C++ source files. I'm using CMake to help with compilation. When running make myproject, it correctly creates the library's object files in build/CMakeFiles/myproject.dir, and these are linked to create the static library build/libmyproject.a (just like I believe it's supposed to).
Root also contains the test file run.cpp, which depends on the library. However, when instead I run make run, after building the objects and linking the library (if needed), the same objects are created a second time, this time in build/CMakeFiles/run.dir.
I suspect that make does not see the library at all. Is this normal/intended behaviour? If not, how do I fix it?
Here's the CMakeLists.txt I'm using:
cmake_minimum_required(VERSION 3.13.0)
project(myproject)
find_package(PkgConfig REQUIRED)
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(BASEPATH "${CMAKE_SOURCE_DIR}")
add_library(myproject)
add_subdirectory(src) # source files...
add_subdirectory(proto) # included from here
target_include_directories(myproject PUBLIC
# ...several library file paths
)
target_link_directories(myproject PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/lib/protobuf/src/.libs
)
target_link_libraries(myproject tbb pthread protobuf)
target_compile_options(myproject PUBLIC -D_REENTRANT -fPIC)
add_executable(run run.cpp)
target_link_libraries(run myproject)
target_compile_options(run PUBLIC -D_REENTRANT -fPIC)
Note: the target_sources() are included via some CMakeLists.txt files in the subfolders. More specifically, in src there is CMakeLists.txt (with content such as add_subdirectory(fold)) and some subfolders, such as fold, which each include a CMakeLists.txt (with content target_sources(myproject PUBLIC file.hpp file.cpp)).
I tried adding target_link_directories(run PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/build), but it didn't do anything.
Thanks for your help.
EDIT: solution is in my last comment.