Link to static library by cmake

3.4k views Asked by At

I have a project C++ using libnuma library. Because I don't have permission to install libnuma in the root system, so I have to install it in folder of user: /home/khangtg/opt. This folder contains 2 main folders:

  • Folder include contains: numacompat1.h, numa.h, numaif.h
  • Folder lib contains: libnuma.a, libnuma.la, libnuma.so, libnuma.so.1, libnuma.so.1.0.0

Now, I have a file .cpp include libnuma library:

#include <numa.h>

and I build the project by file CMakeLists.txt with content:

add_library (common Bigraph.cpp AdjList.cpp Vocab.cpp NumaArray.cpp clock.cpp)
set (LINK_LIBS ${LINK_LIBS} common gflags numa )

add_executable (warplda main.cpp lda.cpp warplda.cpp)
add_executable (format format.cpp)

target_link_libraries (warplda ${LINK_LIBS})
target_link_libraries (format ${LINK_LIBS})

After run cmake command, I get some error that is "can not include numa.h".

So, how can I fix this error and build the project by cmake. Many thanks!

2

There are 2 answers

1
Marcus Karpoff On BEST ANSWER

you want to set the link_directories to include the directory of the libraries. More can be found in the cmake docs. This tells the linker where to look for the libraries.

It should probably look something like this

link_directories(/home/khangtg/opt/lib)

Also add the include directories command from this documentation. This will look like this

include_directories(/home/khangtg/opt/include)
1
Carlos On

This might be useful to add to your cmake build file:

include_directories("/home/khangtg/opt/include")

From: cmake tutorial

You may also want to change the include to :

#include "numa.h"