Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combined find_package/add_subdirectory not linking libraries #1771

Closed
spmcv opened this issue Oct 2, 2019 · 2 comments
Closed

Combined find_package/add_subdirectory not linking libraries #1771

spmcv opened this issue Oct 2, 2019 · 2 comments
Labels
kind: question state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@spmcv
Copy link

spmcv commented Oct 2, 2019

Describe what you want to achieve.

I'm trying to follow the CMake example of Supporting Both.

Describe what you tried.

The external example works as expected. Unfortunately, the former encounters a namespace error. With extern/CMakeLists.txt

if(USE_EXTERNAL_JSON)
    message(DEBUG " using external library for nlohmann JSON")
    find_package(nlohmann_json 3.2.0 REQUIRED)
else()
    message(DEBUG " using git submodule for nlohmann JSON")
    set(JSON_BuildTests OFF CACHE INTERNAL "")
    add_subdirectory(nlohmann_json)
endif()

and CMakeLists.txt:

option(USE_EXTERNAL_JSON "Use an external nlohmann JSON library" ON)
# ...
add_subdirectory(extern)
# ...
target_link_libraries(${MY_TARGET} PRIVATE "nlohmann_json::nlohmann_json")

I get an error:

  Target "MyTarget" links to target "nlohmann_json::nlohmann_json" but
  the target was not found.  Perhaps a find_package() call is missing for an
  IMPORTED target, or an ALIAS target is missing?

However, when removing the namespace:

# ...
target_link_libraries(${MY_TARGET} PRIVATE "nlohmann_json")

Configure throws no error, but now build cannot find the header.

fatal error: 'nlohmann/json.hpp' file not found
#include <nlohmann/json.hpp>
         ^~~~~~~~~~~~~~~~~~~

Describe which system (OS, compiler) you are using.

macOS 10.14.6
CMake 3.14.5

Homebrew

$ brew --version
Homebrew 2.1.11
Homebrew/homebrew-core (git revision ab0ed; last commit 2019-10-02)
Homebrew/homebrew-cask (git revision ccc697; last commit 2019-10-02)

Describe which version of the library you are using.

nlohmann_json

$ brew info nlohmann-json
nlohmann-json: stable 3.7.0 (bottled), HEAD
JSON for modern C++
https://github.com/nlohmann/json
/usr/local/Cellar/nlohmann-json/3.7.0 (8 files, 1MB) *
  Poured from bottle on 2019-09-26 at 17:09:43
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/nlohmann-json.rb
==> Dependencies
Build: cmake ✘
==> Options
--HEAD
	Install HEAD version
@stale
Copy link

stale bot commented Nov 1, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Nov 1, 2019
@stale stale bot closed this as completed Nov 8, 2019
@martin-schulze-vireso
Copy link

Just in case someone stumbles upon this as I did:
I saw the exact same symptoms in an android build where a gradle project pulled in my own library via externalNativeBuild that in turn should pull in this library via add_subdirectory.

It turned out that I confused myself with the hierarchy of my larger project and only built the part of my CMake project tree which did not contain the add_subdirectory for this library. (Part of) my rough project layout:

parent/
  CMakeLists.txt
  third_party/nlohman_json/
   ...
   CMakeLists.txt
  my_library/
    CMakeLists.txt

The add_subdirectory(third_party/nlohmann_json) was done in parent/CMakeLists.txt but gradle ran cmake only on my_library/CMakeLists.txt, so it never saw that. After moving add_subdirectory(../third_part/nlohmann_json ${CMAKE_CURRENT_BINARY_DIR)/nlohmann_json) into my_library/CMakeLists.txt it worked as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

2 participants