CMake not finding header files

This is a very basic CMake question. I installed xlsxwriter library with brew install xlsxwriter, which adds the library to the system: /usr/local/lib/libxlsxwriter.dylib and the header (a symlink) to /usr/local/include/xlsxwriter.h.
Then, I have the following minimal CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project(main)
add_executable(${PROJECT_NAME} "main.cpp")
find_library(XLSXWRITER_LIB xlsxwriter)
message(${XLSXWRITER_LIB})
target_link_libraries(${PROJECT_NAME} PUBLIC ${XLSXWRITER_LIB})

I can verify by the message that the system correctly finds the library.
Then, to test, I have this minimum main.cpp:

#include <xlsxwriter.h>

int main() { return 0; }

However, trying to run it produces the following error: "fatal error: ‘xlsxwriter.h’ file not found".

Can anyone help me see what I’m not doing right?

Thanks

>Solution :

libxlsxwriter ships with a pkg-config file, so you can just use the PkgConfig module:

find_package(PkgConfig REQUIRED)
pkg_check_modules(Xlsxwriter REQUIRED IMPORTED_TARGET xlsxwriter)
target_link_libraries(${PROJECT_NAME} PUBLIC PkgConfig::Xlsxwriter)

This automatically sets up the linker path, linker libraries, and include path for you.

Leave a Reply