cpm-cmake / CPM.cmake

📦 CMake's missing package manager. A small CMake script for setup-free, cross-platform, reproducible dependency management.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I use libraries headers in multiple files?

zagoli opened this issue · comments

Hi, sorry if this question sounds silly or isn't exactly related to cpm but it's one of my first times using c++ in general.
I have this problem: I am creating an executable with three source files, and I want to include a header file from a library added with cpm in all the three sources. However, the header is only visible in the first source (main.cpp). I tried to follow the examples, but they only show how to use the headers in main.cpp. Any help is appreciated! Examples below:

CMakeLists.txt

# initial stuff...
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
set(CPM_SOURCE_CACHE
        "${CMAKE_SOURCE_DIR}/third_party/cpm_sources"
        CACHE PATH "Directory to download CPM dependencies"
        FORCE)
# Include dependency manager
include(CPM)
CPMAddPackage(
        NAME argparse
        GITHUB_REPOSITORY p-ranav/argparse
        VERSION 2.9)
add_executable(cmapd main.cpp AmbientMap.cpp AmbientMap.h)
target_link_libraries(cmapd PRIVATE argparse::argparse)

main.cpp

#include <argparse/argparse.hpp> // Ok!
#include <filesystem>
#include <string>

int main(int argc, char* argv[]) {

AmbientMap.cpp

#include <filesystem>
#include <argparse/argparse.hpp> // Not found!

#include "Point.h"
#include "AmbientMap.h"

AmbientMap::AmbientMap(const std::filesystem::path& path_to_map) {

The same thing occours with fmt library.
I am using clang version 14.0.0-1ubuntu1 on Ubuntu.

Looks good to me, could you share the exact error message you are getting? Also might be interesting to see the output of a clean build using make VERBOSE=1 to see the arguments passed to the compiler.

Ok, that was stupid of me: I actually tried building the project and everything was fine.
The problem is that my IDE (Clion) can't find the headers! This is really annoying, if you have any suggestions on this, they are more than welcome.

I created a small test project in Clion, with only the necessary files to document this behaviour and everything works. I guess than it's a problem with caches / temporary file of my original project.