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?
Asked
Active
Viewed 2,694 times
1

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

Kyrylo Onufriiev
- 41
- 1
- 2
-
2What did you set in your `include_directories` list? – user0042 Oct 14 '17 at 12:16
-
Unrelated to your problem, but if the library is built as part of your project, don't use the library name but the *target* name instead. – Some programmer dude Oct 14 '17 at 12:17
-
I'm not using `include_directories` – Kyrylo Onufriiev Oct 14 '17 at 12:19
-
1That is your problem. Unless they are in system header paths you will need to add include_directories() for the headers for your library. – drescherjm Oct 14 '17 at 12:29
-
@KyryloOnufriiev Have a look at this question. https://stackoverflow.com/questions/924485/whats-the-difference-between-a-header-file-and-a-library It may help you understand what the problem is. – Cinder Biscuits Oct 14 '17 at 12:37
1 Answers
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