CMake build only subprojects, but not top-level project - c++

There is top-level project named 'cppserial', which depends on subproject 'cppstreams', which also depends on subproject 'cpputils'.
The file structure of whole project:
cppserial/
|
CMakeLists.txt
src/
|
cppserial/
source files
libs/
|
cppstreams/
|
CMakeLists.txt
libs/
|
cpputils/
|
CMakeLists.txt
src/
|
cpputils/
source files
src/
|
cppstreams/
source files
cppserial CMakeLists.txt:
cmake_minimum_required(VERSION 3.8)
project(cppserial)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(SOURCE ...sources...)
add_library(${PROJECT_NAME} STATIC ${SOURCE})
add_subdirectory(libs/cppstreams)
target_link_libraries(${PROJECT_NAME} PUBLIC cppstreams)
target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src")
cppstreams CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project(cppstreams)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(SOURCES ...sources...)
set(HEADERS ...headers...)
set(BOOST_ROOT D:/Development/CXX/Libraries/Boost)
find_package(Boost REQUIRED)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME ON)
add_library(${PROJECT_NAME} STATIC ${HEADERS} ${SOURCES})
add_subdirectory(libs/cpputils)
target_link_libraries(${PROJECT_NAME} PUBLIC cpputils)
target_include_directories(${PROJECT_NAME} PUBLIC ${Boost_INCLUDE_DIR})
target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src")
cpputils CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project(cpputils)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(SOURCES ...sources...)
set(HEADERS ...headers...)
add_library(${PROJECT_NAME} STATIC ${HEADERS} ${SOURCES})
target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src")
Building:
cd build
cmake ..
cmake --build .
Build output:
[main] Configuring folder: cppserial
[proc] Executing command: D:\Software\CMake\bin\cmake.EXE --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -Sd:/Development/CXX/cppserial -Bd:/Development/CXX/cppserial/build -G "Visual Studio 17 2022"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044.
[cmake] -- The C compiler identification is MSVC 19.33.31630.0
[cmake] -- The CXX compiler identification is MSVC 19.33.31630.0
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Check for working C compiler: D:/Software/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe - skipped
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Check for working CXX compiler: D:/Software/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe - skipped
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- Found Boost: D:/Development/CXX/Libraries/Boost/lib/cmake/Boost-1.80.0/BoostConfig.cmake (found version "1.80.0")
[cmake] -- Configuring done
[cmake] -- Generating done
[cmake] -- Build files have been written to: D:/Development/CXX/cppserial/build
[visual-studio] Patch Windows SDK path from C:\Program Files (x86)\Windows Kits\10\bin\x64 to C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64 for D:\Software\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat
Compiling output:
MSBuild version 17.3.1+2badb37d1 for .NET Framework
Checking Build System
Building Custom Rule D:/Development/CXX/cppserial/libs/cppstreams/libs/cpputils/CMakeLists.txt
...source files...
cpputils.vcxproj -> D:\Development\CXX\cppserial\build\libs\cppstreams\libs\cpputils\Debug\cpputils.lib
Building Custom Rule D:/Development/CXX/cppserial/libs/cppstreams/CMakeLists.txt
...source files...
cppstreams.vcxproj -> D:\Development\CXX\cppserial\build\libs\cppstreams\Debug\cppstreams.lib
Building Custom Rule D:/Development/CXX/cppserial/CMakeLists.txt
Building Custom Rule D:/Development/CXX/cppserial/CMakeLists.txt
As you can see, only subprojects are being built. There is no cppserial.lib in the build folder.
I can't understand what could i miss.

Related

cmake fails to compile a gd mod (macos)

I wanted to make a GD mod using c++, but when it compiles, i get this error:
[cmake] CMake Error at CMakeLists.txt:5 (project):
[cmake] Generator
[cmake]
[cmake] Unix Makefiles
[cmake]
[cmake] does not support platform specification, but platform
[cmake]
[cmake] darwin
[cmake]
[cmake] was specified.
[cmake]
[cmake]
[cmake] Not searching for unused variables given on the command line.
[cmake] -- Configuring incomplete, errors occurred!
𝅺𝅺𝅺𝅺𝅺𝅺𝅺𝅺𝅺𝅺𝅺𝅺𝅺𝅺𝅺𝅺𝅺𝅺𝅺𝅺𝅺𝅺𝅺𝅺𝅺𝅺
my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.3.0)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project(IlliaGDMod VERSION 1.0.0)
# Use GLOB_RECURSE instead of GLOB
# to recursively add all source files
# under src/
file(GLOB SOURCES
src/*.cpp
)
# Set up the mod binary
add_library(${PROJECT_NAME} SHARED ${SOURCES})
if (NOT DEFINED ENV{GEODE_SDK})
message(FATAL_ERROR "Geode SDK not found")
else()
message(STATUS "Found Geode: $ENV{GEODE_SDK}")
endif()
set(GEODE_LINK_NIGHTLY ON)
add_subdirectory($ENV{GEODE_SDK} $ENV{GEODE_SDK}/build)
target_link_libraries(${PROJECT_NAME} geode-sdk)
create_geode_file(${PROJECT_NAME})
I used VSCode 1.72.2, compiler clang
Does anyone have any ideas on why this happens?
also, i used the visual studio code cmake tools to compile it

Set packages location for CMake

Trying to build samples/mqtt/basic_pub_sub sample from aws-iot-device-sdk-cpp-v2 which contains CMakeList.txt:
cmake_minimum_required(VERSION 3.1)
# note: cxx-17 requires cmake 3.8, cxx-20 requires cmake 3.12
project(basic-pub-sub CXX)
file(GLOB SRC_FILES
"*.cpp"
)
add_executable(${PROJECT_NAME} ${SRC_FILES})
set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_STANDARD 14)
#set warnings
if (MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /WX /wd4068)
else ()
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wno-long-long -pedantic -Werror)
endif ()
find_package(aws-crt-cpp REQUIRED)
target_link_libraries(${PROJECT_NAME} AWS::aws-crt-cpp)
Trying to build:
mkdir build
cd build
cmake ..
Got errro:
-- The CXX compiler identification is GNU 9.3.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:21 (find_package):
By not providing "Findaws-crt-cpp.cmake" in CMAKE_MODULE_PATH this project
has asked CMake to find a package configuration file provided by
"aws-crt-cpp", but CMake did not find one.
Could not find a package configuration file provided by "aws-crt-cpp" with
any of the following names:
aws-crt-cppConfig.cmake
aws-crt-cpp-config.cmake
Add the installation prefix of "aws-crt-cpp" to CMAKE_PREFIX_PATH or set
"aws-crt-cpp_DIR" to a directory containing one of the above files. If
"aws-crt-cpp" provides a separate development package or SDK, be sure it
has been installed.
-- Configuring incomplete, errors occurred!
I found aws-crt-cpp-config.cmake if folder home\a\cpp_projects\sdk-cpp-workspace\lib\aws-crt-cpp\cmake
But how to tell about it to CMake?

How to specify header-only boost library for CMake?

I am using the following cmake file
cmake_minimum_required(VERSION 3.16)
project(Translated01)
find_package(Boost REQUIRED)
find_package(OpenSSL REQUIRED)
set(CMAKE_CXX_STANDARD 14)
add_executable(Translated01 main.cpp)
During CMake configure phase it writes
...
[cmake] -- Check for working CXX compiler: /usr/bin/g++-7 - skipped
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- Found Boost: /opt/boost (found version "1.73.0")
[cmake] -- Found OpenSSL: /usr/lib/x86_64-linux-gnu/libcrypto.so (found version "1.1.1")
[cmake] -- Configuring done
[cmake] -- Generating done
Which proves it is finding boost in /opt/boost.
Unfortunately, during compile it says it can't find include file
[build] ../main.cpp:3:10: fatal error: boost/beast.hpp: No such file or directory
[build] #include <boost/beast.hpp>
[build] ^~~~~~~~~~~~~~~~~
[build] compilation terminated.
Although, file is there
$ ls /opt/boost/boost/beast.hpp
/opt/boost/boost/beast.hpp
So, how to configure boost for CMake correctly?

Cmake - fatal error LNK1104: cannot open file 'libboost_date_time-vc142-mt-gd

I am trying to build my project on Windows. On Linux it works perfectly fine.
Here is my main CMakeLists.txt file:
cmake_minimum_required(VERSION 3.16)
project(Vibranium_Core)
set(CMAKE_CXX_STANDARD 17)
set(FLATBUFFERS_MAX_PARSING_DEPTH 16)
set(FLATBUFFERS_LOCATION "E:/vcpkg/packages/flatbuffers_x86-windows")
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")
endif()
if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
execute_process(
COMMAND git rev-list --count HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(
COMMAND git rev-parse --abbrev-ref HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_BRANCH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(
COMMAND git log -1 --format=%h
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(
COMMAND git --no-pager log -1 --format=%ai
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_RELEASED_ON
OUTPUT_STRIP_TRAILING_WHITESPACE
)
else(EXISTS "${CMAKE_SOURCE_DIR}/.git")
set(GIT_BRANCH "")
set(GIT_COMMIT_HASH "")
endif(EXISTS "${CMAKE_SOURCE_DIR}/.git")
message(STATUS "VibraniumCore current branch: ${GIT_BRANCH}")
message(STATUS "VibraniumCore Version: ${GIT_VERSION}")
message(STATUS "VibraniumCore commit hash: ${GIT_COMMIT_HASH}")
message(STATUS "Released on: ${GIT_RELEASED_ON}")
message(STATUS "Generating version.h")
configure_file(
${CMAKE_SOURCE_DIR}/cmake/version.h.in
${CMAKE_SOURCE_DIR}/Source/Common/Version.h
)
find_package(Boost 1.72.0)
find_package(Threads)
add_definitions(-DGIT_COMMIT_HASH="${GIT_COMMIT_HASH}")
add_definitions(-DGIT_BRANCH="${GIT_BRANCH}")
add_definitions(-DGIT_VERSION="${GIT_VERSION}")
add_definitions(-DGIT_RELEASED_ON="${GIT_RELEASED_ON}")
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
include_directories(${Boost_INCLUDE_DIR})
if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
message(STATUS "Target is 64 bits")
if (WIN32)
set(WINXXBITS Win64)
endif(WIN32)
else("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
message(STATUS "Target is 32 bits")
if (WIN32)
set(WINXXBITS Win32)
endif(WIN32)
endif("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
find_package(MySQL REQUIRED)
if(WIN32)
find_package(Flatbuffers REQUIRED
PATHS ${FLATBUFFERS_LOCATION})
else()
find_package(Flatbuffers REQUIRED
PATHS /usr/local/flatbuffers)
endif()
include_directories(${MYSQL_INCLUDE_DIR})
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Source/Common)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Source/Core/WorldServer)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Source/Core/AuthServer)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Source/ClientEmulator)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Source/Game)
add_subdirectory(Tests)
set_target_properties(VibraniumCoreTests PROPERTIES EXCLUDE_FROM_ALL TRUE)
set_target_properties(gtest PROPERTIES EXCLUDE_FROM_ALL TRUE)
set_target_properties(gmock PROPERTIES EXCLUDE_FROM_ALL TRUE)
set_target_properties(gtest_main PROPERTIES EXCLUDE_FROM_ALL TRUE)
set_target_properties(gmock_main PROPERTIES EXCLUDE_FROM_ALL TRUE)
set_target_properties(
Common WorldServer AuthServer ClientEmulator Game
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/lib"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/lib"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)
Here is my Common CMakeLists.txt:
file(GLOB flatBufferFiles_SRC
"Server/Packets/*/*.h"
"Server/Packets/*/*.cc"
)
add_library(
Common
SHARED
Config.cpp
Config.h
Logger.cpp
Logger.h
Database/MySQLConnection.h
Database/MySQLConnection.cpp
Database/MySQLTable.h
Database/MySQLTable.cpp
Banner.cpp
Banner.h
Database/MySQLTableBase.cpp
Database/MySQLTableBase.h
Memory/Memory.cpp
Memory/Memory.h
Server/Server.cpp
Server/Server.h
Server/Client.cpp
Server/Client.h
Server/Client.cpp
Server/Client.h
DataSchemas/Account.h
DataSchemas/AccountRole.h
${flatBufferFiles_SRC}
Helpers/Timer.h
Crypto/sha512.h
Server/Packet.cpp Server/Packet.h Server/Protocol/ServerOpcode.h Server/Protocol/ClientOpcode.h Server/Protocol/Opcodes.cpp Server/Protocol/Opcodes.h Server/WorldSession.cpp Server/WorldSession.h Server/AuthSession.cpp Server/AuthSession.h)
target_include_directories(Common PUBLIC ${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/include ${FLATBUFFERS_LOCATION}/include)
target_include_directories(Common PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${MYSQL_LIBRARY} ${CMAKE_CURRENT_SOURCE_DIR}/Server/Packets)
target_link_libraries(Common PUBLIC ${MYSQL_CONNECTOR_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES})
When I run cmake I get the following:
-- The C compiler identification is MSVC 19.24.28315.0
-- The CXX compiler identification is MSVC 19.24.28315.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- VibraniumCore current branch: windows
-- VibraniumCore Version: 161
-- VibraniumCore commit hash: d8d564d
-- Released on: 2020-10-26 11:26:24 +0200
-- Generating version.h
-- Found Boost: C:/local/boost_1_72_0 (found suitable version "1.72.0", minimum required is "1.72.0")
-- Looking for pthread.h
-- Looking for pthread.h - not found
-- Found Threads: TRUE
-- Target is 64 bits
-- New WorldServer Config file in will be installed in: E:/Vibranium-Core/cmake-build-debug/bin/configs
-- New AuthServer Config file in will be installed in: E:/Vibranium-Core/cmake-build-debug/bin/configs
-- Found PythonInterp: C:/Program Files (x86)/Python38-32/python.exe (found version "3.8.5")
-- Configuring done
-- Generating done
-- Build files have been written to: E:/Vibranium-Core/cmake-build-debug
[Finished]
So boost is found. However when I try to build I get the following error:
====================[ Build | AuthServer | Debug ]==============================
"C:\Program Files\JetBrains\CLion 2020.1.3\bin\cmake\win\bin\cmake.exe" --build E:\Vibranium-Core\cmake-build-debug --target AuthServer
[ 4%] Linking CXX shared library ..\..\bin\Common.dll
LINK Pass 1: command "C:\PROGRA~2\MICROS~1\2019\COMMUN~1\VC\Tools\MSVC\1424~1.283\bin\Hostx64\x64\link.exe /nologo #CMakeFiles\Common.dir\objects1.rsp /out:..\..\bin\Common.dll /implib:..\..\bin\lib\Common.lib /pdb:E:\Vibranium-Core\cmake-build-debug\bin\Common.pdb /dll /version:0.0 /machine:x64 /debug /INCREMENTAL C:\Program Files\MySQL\Connector C++ 8.0\lib64\vs14\mysqlcppconn8.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\Common.dir/intermediate.manifest CMakeFiles\Common.dir/manifest.res" failed (exit code 1104) with the following output:
LINK : fatal error LNK1104: cannot open file 'libboost_date_time-vc142-mt-gd-x64-1_72.lib'
NMAKE : fatal error U1077: '"C:\Program Files\JetBrains\CLion 2020.1.3\bin\cmake\win\bin\cmake.exe"' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
I checked and I have this file libboost_date_time-vc142-mt-gd-x64-1_72.lib it is located in C:\local\boost_1_72_0\lib64-msvc-14.2
Also when I do :
message("BOOST LIBRARIES LOCATION: " ${Boost_LIBRARIES})
It comes up BOOST LIBRARIES LOCATION: so this variable on Windows ${Boost_LIBRARIES} is empty. Why is that and why it works on Linux but not on Windows ?
Most but not all Boost libraries are header only. Boost date_time is not therefore it needs to be added to your find_package command for Boost.
find_package(Boost 1.72.0 COMPONENTS date_time)
This will find the Boost DLLs on windows.
If you are interested in the static libraries you need to add
set(Boost_USE_STATIC_LIBS ON)
before your find_package_command.
After applying the changes to your CMakeLists.txt file clear the CMake cache, i.e. delete the CMakeCache.txt file in your build directory and rerun CMake.
If you are using Visual Studio, Have you tried using Tools / NuGet Package Manager to install boost_program_options-vc142? That worked for me.

cmake error for header-only library: `include could not find load file`

I've been trying to change a makefile c++ project into a cmake project, and I've been having som difficulty. cmake seems to be looking for stuff in /usr/local/lib/ instead of /usr/local/include/ and I'm not sure why that is.
This library is header-only, and so I've been following this tutorial My header-only library in include seems to "build" fine, but I keep getting the following error when I try to generate a makefile to build my example program:
me:~/pf/examples/build$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at /usr/local/lib/cmake/pf/pfConfig.cmake:27 (include):
include could not find load file:
/usr/local/lib/cmake/pf/pf_exampleTargets.cmake
Call Stack (most recent call first):
CMakeLists.txt:15 (find_package)
examples/CMakeLists.txt creates another fresh project:
project(pf_example)
cmake_minimum_required (VERSION 3.12)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
set(CMAKE_CXX_FLAGS "-Wall -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(CMAKE_CXX_STANDARD 17)
# "install" pf
find_package(pf CONFIG REQUIRED)
find_package(Eigen3 3.3 REQUIRED NO_MODULE)
file(GLOB SOURCES ${PROJECT_NAME}/*.{h,cpp})
message("${SOURCES}")
add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME} pf::pf)
The root directory CMakeLists.txt file is more complicated. It's the one that was adapted from the tutorial I mentioned above:
cmake_minimum_required(VERSION 3.12)
project("pf" VERSION 1.0.1
DESCRIPTION "A header only c++ template library for fast particle filtering."
HOMEPAGE_URL "https://github.com/tbrown122387/pf")
include(GNUInstallDirs)
add_library(${PROJECT_NAME} INTERFACE)
target_include_directories(
${PROJECT_NAME}
INTERFACE $<BUILD_INTERFACE:${${PROJECT_NAME}_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_17)
install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}_Targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
include(CMakePackageConfigHelpers)
write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)
configure_package_config_file(
"${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake.in"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION
${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
install(EXPORT ${PROJECT_NAME}_Targets
FILE ${PROJECT_NAME}Targets.cmake
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include DESTINATION include)
To install this header only library, before I try to build the examples project, I typed the following commands into the command line:
cd ~/pf
mkdir build
cd build/
cmake .. -DCMAKE_INSTALL_PREFIX:PATH=/usr/local/include
sudo cmake --build . --config Release --target install -- -j $(nproc)
I didn't post it, but there's also a file cmake/pfconfig.cmake.in that is verbatim copied from the tutorial above.
Your install prefix is specified as /usr/local/include so the files would be installed as:
headers into /usr/local/include/include
libraries into /usr/local/include/libs
cmake stuff into /usr/local/include/share/${PROJECT_NAME}/cmake
Those paths are just wrong. Just set CMAKE_INSTALL_PREFIX=/usr/local (ie. remove include) and install it inside /usr/local/ tree.
A few took care of this issue.
in CMakeLists.txt change install(DIRECTORY ${PROJECT_SOURCE_DIR}/include DESTINATION include) to install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/pf DESTINATION include) so the files don't clutter up the installation locations. This also requires creating pf/include/pf and moving all the files in pf/include to pf/include/pf.
Follow advice in #KamilCuk's answer.
In examples/CMakeLists.txt change file(GLOB SOURCES ${PROJECT_NAME}/*.{h,cpp}) to file(GLOB SOURCES ${CMAKE_SOURCE_DIR}/*.h ${CMAKE_SOURCE_DIR}/*.cpp)
Also note that my /usr/local/lib and /usr/local/include were quite cluttered up due to my numerous earlier failed attempts, so I deleted a bunch of files in there and re-installed fresh.