0

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.

Brunjox
  • 1
  • 2
  • For reuse object files, use `OBJECT` library. Otherwise CMake is intended to create different object files when build different targets. – Tsyvarev Nov 22 '20 at 22:47
  • The duplicate question only addresses reusing libraries in other libraries, not in executables. Same as elsewhere, by the way. Adding `add_library(myproject OBJECT)` and `add_executable(run $ run.cpp)` does not solve the issue. Am I missing something else? – Brunjox Nov 23 '20 at 13:16
  • Well, which **exact file** is compiled twice? Please, show ([edit] the code in your question post) how do you add this file into the library. Currently you create a library without the sources - `add_library(myproject)` - and have no `target_sources` command for add sources to the library. – Tsyvarev Nov 23 '20 at 13:47
  • All of the objects of the library, in fact. Of course, I mean only the ones which were modified since the last build (as per correct make behaviour), which means all of them for a fresh build. For example, `file.cpp.o` corresponding to `src/fold/file.cpp`. I've edited the post so as to clarify where source files come from. – Brunjox Nov 23 '20 at 14:06
  • Hm, OBJECT library should perfectly work in that case... Could you prepare [mcve] which reproduces your problem? With smaller code you don't need to describe "my code in the file XXX does ..." but just post that code. A *description* of the code is imprecise sometimes... – Tsyvarev Nov 23 '20 at 14:27
  • @tsyvarev Sorry for the late reply. It turns out that extracting a _minimal_ reproducible example out of my project is quite tough. On the bright side, while removing random CMake flags to achieve _minimality_, I actually figured out the solution! The problem was `target_link_libraries(run myproject)`, which of course mandated that the library be built once more. By removing that line _and_ adding the same include paths and link libraries as the ones for `myproject`, everything started working like you said it would. Thanks a bunch for your time ^^ – Brunjox Nov 24 '20 at 23:33

0 Answers0