I developed a library with lib.c
and lib.h
, which has its own CMakeLists.txt
file, but to properly operate, this library includes lib_options.h
file from the lib.h
header. lib_options.h
file must be provided by the user.
lib.h:
#ifndef LIB_HDR_H
#define LIB_HDR_H
//This file is user provided and is project specific
#include "lib_options.h"
#ifdef ...
#endif /* ... */
typedef enum {...} enum_name_t;
#endif
lib.c
#include "lib.h"
By far the easiest way is in fact (but I suspect super wrong) to use the cmake with INTERFACE library type, like:
add_library(lib INTERFACE)
target_sources(lib INTERFACE lib.c)
target_include_directories(lib INTERFACE lib.h)
And then it can be used in top CMakeLists.txt
as:
add_executable(exec_name)
add_subdirectory(path/to/my/lib)
target_link_libraries(exec_name lib)
target_include_directories(exec_name .)
and if I create a lib_options.h
file in the root directory, this one will be automatically picked up and compilation will work.
Reading several posts, use of INTERFACE shall be used for I don’t need header, but whoever is calling me, may need it type of include.
But if I change the interface to PUBLIC
, then lib
won’t picked up paths where user’s lib_options.h
is stored, and will return a missing file error during compilation
How to properly setup this?
1