cmake无法链接可执行文件-LJSONCPP:没有使用github子模块的此类文件

发布于 2025-02-03 01:26:56 字数 1200 浏览 3 评论 0原文

我正在一个使用JSONCPP进行解析和CMAKE进行编译的项目。我添加了JSONCPP官方git repository 作为我的项目的子模型subpoule添加repo_url外部/jsoncpp ,以使每个依赖关系保持在一起。

运行cmake -b out/build时,它正常工作。但是,当我做make时,我会收到以下错误:

/usr/bin/ld:找不到-ljsoncpp:no这样的文件或目录

文件以下方式组织了:

- root
    - out/build
    - external
        - jsoncpp (cloned repo)
    - include
        foo.h
        bar.h
    - src
        foo.cpp
        bar.cpp
        main.cpp
    CMakeLists.txt

cmakelists.txt是这样:

cmake_minimum_required(VERSION 3.22.1)
project(ants)


# ".cpp" files in folder "src" into cmake variable "SOURCE"
file(GLOB SOURCE "src/*.cpp")

# Executable
add_executable(${PROJECT_NAME} ${SOURCE})

# Directory where cmake will look for include files
include_directories(include)

# Tells cmake to compile jsoncpp
add_subdirectory(external/jsoncpp)
# Tells cmake where to look for jsoncpp include files
target_include_directories(${PROJECT_NAME} 
    PUBLIC external/jsoncpp/include 
)

target_link_libraries(${PROJECT_NAME} jsoncpp)

I am working in a project which uses jsoncpp for parsing and cmake for compilation. I added the jsoncpp official git repository as a submodule to my project with git submodule add REPO_URL external/jsoncpp, so as to keep every dependency together.

When running cmake -B out/build, it works normally. But when I do make, I get the following error:

/usr/bin/ld: cannot find -ljsoncpp: No such file or directory.

The files are organized the following way:

- root
    - out/build
    - external
        - jsoncpp (cloned repo)
    - include
        foo.h
        bar.h
    - src
        foo.cpp
        bar.cpp
        main.cpp
    CMakeLists.txt

The CMakeLists.txt is like this:

cmake_minimum_required(VERSION 3.22.1)
project(ants)


# ".cpp" files in folder "src" into cmake variable "SOURCE"
file(GLOB SOURCE "src/*.cpp")

# Executable
add_executable(${PROJECT_NAME} ${SOURCE})

# Directory where cmake will look for include files
include_directories(include)

# Tells cmake to compile jsoncpp
add_subdirectory(external/jsoncpp)
# Tells cmake where to look for jsoncpp include files
target_include_directories(${PROJECT_NAME} 
    PUBLIC external/jsoncpp/include 
)

target_link_libraries(${PROJECT_NAME} jsoncpp)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弥繁 2025-02-10 01:26:56

jsoncppconfig.cmake定义属性interface_include_directories for targets jsoncpp_libJSONCPP_LIB_STATIC

您需要查询目标属性并手动设置:

get_target_property(JSON_INC_PATH jsoncpp_lib INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${JSON_INC_PATH})

链接是通过:

target_link_libraries(${PROJECT_NAME} jsoncpp_lib)

尝试以下操作:

cmake_minimum_required(VERSION 3.22.1)
project(ants)

# ".cpp" files in folder "src" into cmake variable "SOURCE"
file(GLOB SOURCE "src/*.cpp")

# Executable
add_executable(${PROJECT_NAME} ${SOURCE})

# Directory where cmake will look for include files
include_directories(include)

# Tells cmake to compile jsoncpp
add_subdirectory(external/jsoncpp)
get_target_property(JSON_INC_PATH jsoncpp_lib INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${JSON_INC_PATH})

target_link_libraries(${PROJECT_NAME} jsoncpp_lib)

The jsoncppConfig.cmake defines property INTERFACE_INCLUDE_DIRECTORIES for targets jsoncpp_lib and jsoncpp_lib_static.

You need to query the target property and set it manually:

get_target_property(JSON_INC_PATH jsoncpp_lib INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${JSON_INC_PATH})

Linking is done via:

target_link_libraries(${PROJECT_NAME} jsoncpp_lib)

Source.

Try this:

cmake_minimum_required(VERSION 3.22.1)
project(ants)

# ".cpp" files in folder "src" into cmake variable "SOURCE"
file(GLOB SOURCE "src/*.cpp")

# Executable
add_executable(${PROJECT_NAME} ${SOURCE})

# Directory where cmake will look for include files
include_directories(include)

# Tells cmake to compile jsoncpp
add_subdirectory(external/jsoncpp)
get_target_property(JSON_INC_PATH jsoncpp_lib INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${JSON_INC_PATH})

target_link_libraries(${PROJECT_NAME} jsoncpp_lib)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文