CMake undefined reference to `mg_printf'

CMake undefined reference to `mg_printf'

我已经尝试解决这个问题一段时间了,我决定寻求帮助。

我想在我的项目中包含猫鼬,所以在我的 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 的建议帮助了我。

你忘了 link 对抗猫鼬。 在 CmakeList.txt:

的末尾添加这一行
target_link_libraries (api_and_json mongoose)