1
Project folder
|\ - Library folder
|     - CMakeLists.txt (1)
|
 \ - Tests folder
      - CMakeLists.txt (2)

I use add_library in (1).
I want to use target_link_libraries( tests library ) in CMakelists.txt (2).

I am completely baffled by CMake. I've been using it for a while but this really trips me up. link_directories, find_library, interfaces, global; they're very confusing.

swiebertje
  • 13
  • 2
  • "I want to use `target_link_libraries( tests library )`" - It is a correct way. Have you tried it? What error it gives? – Tsyvarev Apr 12 '19 at 23:24

1 Answers1

0

You haven't provided much error information or code to really understand the problem, but there are a few potential issues based on your description:

  1. You may be missing a top-level CMakeLists.txt file to tie your CMake files (1) and (2) together.
  2. Inconsistent target/variable naming between your CMake files (1) and (2).

For a project such as this, a typical CMake file hierarchy will include a top-level CMakeLists.txt file with something like:

cmake_minimum_required(VERSION 3.9)
project(MyProject1)

add_subdirectory(LibraryFolder)
add_subdirectory(TestsFolder)

The add_subdirectory commands tell CMake where else to look for additional CMake files and other source code you want built. In this case, CMake will look in the LibraryFolder and TestsFolder directories.

Your other two CMakeLists.txt files may look something like the following:

CMakeLists.txt (in LibraryFolder)

project(MyLibraryProject)
# Define the library, named MyLibrary1, that you want to build here.
add_library(MyLibrary1
    Puppy.cpp
    Puppy.hpp
)

CMakeLists.txt (in TestsFolder)

project(MyTestsProject)
# Define the executable, named MyTestExecutable, that you want to build here.
add_executable(MyTestExecutable
    main.cpp
)
# Link libraries that your executable depends on.
target_link_libraries(MyTestExecutable MyLibrary1)

Be careful to make sure the folder names provided to add_subdirectory match the folder names on your system, and that your CMake target names (e.g. MyLibrary1) are consistent across your files.

When debugging CMake, I always find it helpful to use the message command to print out any variables of interest to the screen. This makes it easier to verify CMake is doing what we expect.

If you still have questions, I suggest reading through the answer here, which provides more context and overview about how to use CMake.

Kevin
  • 16,549
  • 8
  • 60
  • 74
  • Your answer helped a lot, thanks for the clear and detailed explanation. My issue was that I was getting an undefined reference, even after linking. This put me on the wrong foot, as I thought that compile targets like executables and libraries are not accessible in between CMakeLists. The issue ended up being a wrongly setup .cpp file, not faulty linking. – swiebertje Apr 13 '19 at 08:02
  • @swiebertje Glad you got things working! With the CMake build approach, there are several stages at which errors can occur (CMake errors, compile errors, link errors, etc.), and it is often tough to determine where things went wrong. – Kevin Apr 13 '19 at 14:38