【问题标题】:CMake undefined reference to `mg_printf'CMake未定义引用`mg_printf'
【发布时间】:2017-04-18 16:06:39
【问题描述】:

我一直在努力解决这个问题,我决定寻求帮助。

我正在尝试在我的项目中包含猫鼬,所以在我的 api_and_json.cpp 文件中我有:

extern "C"{
    #include "mongoose/mongoose.h"
}

我的 cmake 文件目前如下所示:

cmake_minimum_required(VERSION 2.8)

include(ProcessorCount)
ProcessorCount(N)
if(NOT N EQUAL 0)
    set(CTEST_BUILD_FLAGS -j${N})
    set(ctest_test_args ${ctest_test_args} PARALLEL_LEVEL ${N})
endif()

project(api_and_json)

set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})

include_directories("${PROJECT_SOURCE_DIR}")

add_executable(api_and_json ${PROJECT_SOURCE_DIR}/api_and_json.cpp)

我已经尝试了一些排列,所以我把它剥离回原来的样子。

CMake 工作正常,但是当我尝试 make 时,我得到:

...
[ 50%] Linking CXX executable ../bin/api_and_json
CMakeFiles/api_and_json.dir/api_and_json.cpp.o: In function `ev_handler(mg_connection*, int, void*)':
api_and_json.cpp:(.text+0xa1): undefined reference to `mg_printf'
api_and_json.cpp:(.text+0xb7): undefined reference to `mg_printf_http_chunk'
api_and_json.cpp:(.text+0xcd): undefined reference to `mg_send_http_chunk'
CMakeFiles/api_and_json.dir/api_and_json.cpp.o: In function `main':
api_and_json.cpp:(.text+0x10c): undefined reference to `mg_mgr_init'
api_and_json.cpp:(.text+0x184): undefined reference to `mg_bind_opt'
api_and_json.cpp:(.text+0x1dc): undefined reference to `mg_set_protocol_http_websocket'
api_and_json.cpp:(.text+0x32b): undefined reference to `mg_mgr_poll'
collect2: error: ld returned 1 exit status
CMakeFiles/api_and_json.dir/build.make:94: recipe for target '../bin/api_and_json' failed
make[2]: *** [../bin/api_and_json] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/api_and_json.dir/all' failed
make[1]: *** [CMakeFiles/api_and_json.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

我确定我遗漏了一些简单的东西,我们将不胜感激。

我在其他地方看过,包括: What is an undefined reference/unresolved external symbol error and how do I fix it?

这个参考对我没有帮助,但 Rama 的建议对我有帮助。

【问题讨论】:

标签: c++ makefile cmake mongoose-web-server


【解决方案1】:

你忘记链接猫鼬了。 在 CmakeList.txt 的末尾添加这一行:

target_link_libraries (api_and_json mongoose)

【讨论】:

    猜你喜欢
    • 2020-10-29
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    • 2015-12-06
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多