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:
- You may be missing a top-level
CMakeLists.txt
file to tie your CMake files (1) and (2) together.
- 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.