1

I'm using CLion with CMake. I have my own static library "libxxx.a". I'm trying to link it this way in CMakeLists.txt: target_link_libraries(myProject ./lib/libxxx.a) And this way I'm including library to my main.cpp. #include "xxx.h". But I have error fatal error: xxx.h: No such file or directory. What should I do?

Naseef Chowdhury
  • 2,357
  • 3
  • 28
  • 52

1 Answers1

2

CMake include a prebuilt static library

As a sketch you need your project to look something like this

project( myProject )

set( SOURCE_FILES main.cpp )

add_library( myLibrary STATIC IMPORTED )
set_property( TARGET myLibrary PROPERTY IMPORTED_LOCATION /path/to/lib/libxxx.a )
include_directories( /path/to/headers/ )

add_executable( myProject ${SOURCE_FILES} )
target_link_libraries( myProject myLibrary )

You might be able to replace include_directories with:

set_property( TARGET myLibrary PROPERTY INCLUDE_DIRECTORIES /path/to/headers/ )

A better way to go is to compile the static library from source and then use

target_include_directories( myLibrary PUBLIC /path/to/headers/ )

And then they get handled automatically.

Cameron Lowell Palmer
  • 21,528
  • 7
  • 125
  • 126