带有 AUTOMOC 的 CMake 正在删除我的实现

CMake with AUTOMOC is removing my implementations

我想使用 CMake 构建 this QHttp project,我为构建基本服务器示例编写了一个最小的 CMakeLists.txt 集,但不是 linking。

我为 3rdparty 库 http-parser linked 使用 qhttp 项目创建了一个 OBJECT 库,然后 link 使用示例项目。 我尝试过其他配置,但遇到同样的问题,但这个更具可读性。

也许文件与原始项目的位置不同,但代码基本相同。

我的CMakeLists.txt

第三方/CMakeLists.txt

cmake_minimum_required(VERSION 3.13)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

add_library(http-parser STATIC)

target_sources(http-parser
    PRIVATE
    http-parser/contrib/parsertrace.c
    http-parser/contrib/url_parser.c
    http-parser/http_parser.c
    PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/http-parser/include/http_parser.h
    )

target_include_directories(http-parser PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/http-parser/include)

根目录CmakeLists.txt

cmake_minimum_required(VERSION 3.13)
project(qhttp VERSION 1.0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

set(CMAKE_VERBOSE_MAKEFILE ON)

find_package(Qt5 COMPONENTS Network Core REQUIRED)

add_subdirectory(3rdparty)

add_library(${PROJECT_NAME} SHARED)
target_sources(${PROJECT_NAME}
    PRIVATE
    include/qhttpabstracts.hpp
    include/qhttpserverconnection.hpp
    include/qhttpserverrequest.hpp
    include/qhttpserverresponse.hpp
    include/qhttpserver.hpp
    src/qsslserver.h
    src/qhttpabstracts.cpp
    src/qhttpserverconnection.cpp
    src/qhttpserverrequest.cpp
    src/qhttpserverresponse.cpp
    src/qhttpserver.cpp
    src/qsslserver.cpp
    src/private/httpparser.hxx)

target_link_libraries(qhttp
    PUBLIC
    Qt5::Network Qt5::Core
    http-parser)

target_include_directories(${PROJECT_NAME}
    PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include/qhttp>
    PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/src)

add_subdirectory(example)

基本服务器

cmake_minimum_required(VERSION 3.13)

add_executable(basic-server basic-server/main.cpp)
target_link_libraries(basic-server qhttp)

错误

出于某种原因,它 link 运行不正常,编译器向我发送了下一条消息:

/usr/bin/cmake -E cmake_link_script CMakeFiles/basic-server.dir/link.txt --verbose=1
/usr/bin/g++     CMakeFiles/basic-server.dir/basic-server/main.cpp.o CMakeFiles/basic-server.dir/basic-server_autogen/mocs_compilation.cpp.o  -o basic-server -Wl,-rpath,[...]/build-qhttp-Desktop-Default ../libqhttp.so.1.0.1 /usr/lib/x86_64-linux-gnu/libQt5Network.so.5.11.3 /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.11.3 
/usr/bin/ld: ../libqhttp.so.1.0.1: undefined reference to `http_method_str'
/usr/bin/ld: ../libqhttp.so.1.0.1: undefined reference to `http_parser_init'
/usr/bin/ld: ../libqhttp.so.1.0.1: undefined reference to `http_parser_execute'
collect2: error: ld returned 1 exit status

编辑:更多信息

我得到未定义引用的原因 我认为是因为您可以看到,构建的库中没有符号。

nm libhttp-parser.a 

mocs_compilation.cpp.o:

代码是正确的,因为我是用QMake构建的。它有正确的:

#ifdef __cplusplus
extern "C" {
#endif

但是CMake没有生成构建权。

另一方面,如果我只构建带有 CMakeLists.txt 的 http-parser,我会得到:

nm libhttp-parser.a  

parsertrace.c.o:
[...]
                 U _GLOBAL_OFFSET_TABLE_
0000000000005953 T http_body_is_final
000000000000515c T http_errno_description
0000000000005115 T http_errno_name
0000000000004f0a T http_message_needs_eof
0000000000005021 T http_method_str
00000000000053fa t http_parse_host
00000000000051a3 t http_parse_host_char
000000000000037e T http_parser_execute
000000000000504f T http_parser_init
0000000000005679 T http_parser_parse_url
00000000000058dc T http_parser_pause
00000000000050f0 T http_parser_settings_init
0000000000005654 T http_parser_url_init
0000000000005973 T http_parser_version
0000000000004fa7 T http_should_keep_alive
0000000000000120 d http_strerror_tab
[...]

有符号。我的 CMake 顶级项目有什么问题? MOC 会把我的代码扔进垃圾桶吗?

终于找到问题所在了。 问题是 AUTOMOC 隐藏了真正的错误 unable to determine linker language with C++.

轻松解决:

project(qhttp VERSION 1.0.1 LANGUAGES C CXX)

project(qhttp VERSION 1.0.1)