0

I am trying to link the following library : nngpp using the following commands

mkdir build
cd build
cmake ..
make
make install

However when testing the demos or using the library in a project with the following in CMakeLists.txt :

...
add_executable(target main.cpp)
target_link_libraries(target nngpp)

I get the following error:

fatal error: 'nngpp/nngpp.h' file not found
#include <nngpp/nngpp.h>
         ^~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/rest.dir/rest/server.o] Error 1
make[1]: *** [CMakeFiles/rest.dir/all] Error 2
make: *** [all] Error 2

note : The library is header-only. but I don't want to copy it in my project.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
John C.
  • 405
  • 5
  • 19
  • 2
    Is there a `find_package(nngpp)` somewhere in your CMakeLists.txt? And a finder available for nngpp? – drescherjm Jul 10 '21 at 22:06
  • figure out where to add it as an include path... https://stackoverflow.com/q/13703647/14237276 header only means its a compiler thing and not a linker thing. – Abel Jul 10 '21 at 22:08
  • 1
    `target_link_libraries` makes no sense for a header-only library, as there is nothing to link. – n. m. could be an AI Jul 10 '21 at 22:12
  • @drescherjm yes there is `find_package(nngpp)` above – John C. Jul 10 '21 at 22:24
  • I tried to follow the demos `CMakeLists.txt`. https://github.com/cwzx/nngpp/blob/master/demo/CMakeLists.txt – John C. Jul 10 '21 at 22:26
  • 2
    Demos are NOT standalone projects. README of the library clearly says just to add include directories: "Header-only -- just add the `include` directory to your includes". Find out where the `include` directory is installed. It is probably `/usr/include` or `/usr/local/include`. You may run `make install` for nngpp again, and it will show you all installation paths. – Tsyvarev Jul 11 '21 at 07:45
  • The error suggests a missing include directory, not a linker error. A linker error would give "undefined reference". I would try `include_directories(target )` . – RHertel Jul 11 '21 at 22:07

3 Answers3

0

With

find_package(nngpp)

using the library is straightforward:

add_executable(target main.cpp)
target_link_libraries(target nng::nngpp)

Here nng::nngpp is IMPORTED library, so it cares about include directories for you.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • The fact that `cmake ..` does not give him an error suggests that he already has this part working normally already. – Dan Nestor Jul 11 '21 at 10:59
  • 2
    @DanNestor: No, CMake won't warn you in case you link with non-*namespaced*, non-*existent* library: CMake just passes this library to the linker, with corresponded flag (on Linux it will be `-lnngpp`). Only at the **linking stage** the linker will search the library and will emit an error if it is not found. Note, that error in the question occurs at the **compiling stage**, before the linking one. But CMake will emit an error would one link with **namespaced** library which is not a **target**. E.g. `target_link_libraries(target nng::abc)` will emit an error during `cmake` invocation. – Tsyvarev Jul 11 '21 at 12:03
  • I was using `nngpp::nngpp` instead of `nng::nngpp`. Thanks for your answer it helped – John C. Jul 12 '21 at 17:43
0

It's hard to know exactly what's wrong without more information. I would suggest running make VERBOSE=1. This will show you the command line that is being executed when trying to compile the file that gives you the error.

Look in the command line for include flags (e.g. -I flags if you're using gcc). Find the directory for nngpp and double-check if your header files are in there, and what the correct path relative to that directory is to reference the header file. Maybe you need to only #include <nngpp.h> instead?

Dan Nestor
  • 2,441
  • 1
  • 24
  • 45
0

The following worked :

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(comm)
set(CMAKE_CXX_STANDARD 11)

find_package(nng REQUIRED)
find_package(Threads)
find_package(nngpp REQUIRED)

add_executable(server src/tfo-server.cpp)
target_link_libraries(server nng::nng nng::nngpp)

This also worked:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(comm)
set(CMAKE_CXX_STANDARD 11)


add_executable(server src/tfo-server.cpp)
include_directories(server /usr/local/include)
John C.
  • 405
  • 5
  • 19