Cmake错误构建库

Posted

技术标签:

【中文标题】Cmake错误构建库【英文标题】:Cmake error build lib 【发布时间】:2015-04-03 11:09:48 【问题描述】:

我编写简单的 Protobuf-C 库。在将其编译为 lib 时出现以下错误:

Scanning dependencies of target ArgentumTracer-native
[ 16%] Building C object CMakeFiles/ArgentumTracer-native.dir/src/sources/http_sender.c.o
/Users/rmuhamedgaliev/Projects/ArgentumTracer-native/src/sources/http_sender.c:36:33: warning: incompatible pointer types passing 'in_addr_t *' (aka 'unsigned int *') to parameter of type 'struct in_addr *' [-Wincompatible-pointer-types]
    if ( inet_aton(SERVER_ADDR, &dest.sin_addr.s_addr) == 0 )
                                ^~~~~~~~~~~~~~~~~~~~~
/usr/include/arpa/inet.h:83:47: note: passing argument to parameter here
int              inet_aton(const char *, struct in_addr *);
                                                         ^
1 warning generated.
[ 33%] Building C object CMakeFiles/ArgentumTracer-native.dir/src/sources/main_agent.c.o
[ 50%] Building C object CMakeFiles/ArgentumTracer-native.dir/src/sources/VMInfo.pb-c.c.o
Linking C executable ArgentumTracer-native
[ 50%] Built target ArgentumTracer-native
Scanning dependencies of target argentumtracer
[ 66%] Building C object CMakeFiles/argentumtracer.dir/src/sources/http_sender.c.o
/Users/rmuhamedgaliev/Projects/ArgentumTracer-native/src/sources/http_sender.c:36:33: warning: incompatible pointer types passing 'in_addr_t *' (aka 'unsigned int *') to parameter of type 'struct in_addr *' [-Wincompatible-pointer-types]
    if ( inet_aton(SERVER_ADDR, &dest.sin_addr.s_addr) == 0 )
                                ^~~~~~~~~~~~~~~~~~~~~
/usr/include/arpa/inet.h:83:47: note: passing argument to parameter here
int              inet_aton(const char *, struct in_addr *);
                                                         ^
1 warning generated.
[ 83%] Building C object CMakeFiles/argentumtracer.dir/src/sources/main_agent.c.o
[100%] Building C object CMakeFiles/argentumtracer.dir/src/sources/VMInfo.pb-c.c.o
Linking C shared library libargentumtracer.dylib
Undefined symbols for architecture x86_64:
  "_protobuf_c_message_free_unpacked", referenced from:
      _vminfo__free_unpacked in VMInfo.pb-c.c.o
  "_protobuf_c_message_get_packed_size", referenced from:
      _vminfo__get_packed_size in VMInfo.pb-c.c.o
  "_protobuf_c_message_pack", referenced from:
      _vminfo__pack in VMInfo.pb-c.c.o
  "_protobuf_c_message_pack_to_buffer", referenced from:
      _vminfo__pack_to_buffer in VMInfo.pb-c.c.o
  "_protobuf_c_message_unpack", referenced from:
      _vminfo__unpack in VMInfo.pb-c.c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [libargentumtracer.dylib] Error 1
make[1]: *** [CMakeFiles/argentumtracer.dir/all] Error 2
make: *** [all] Error 2

CMakeLists.txt

cmake_minimum_required(VERSION 3.1)
project(ArgentumTracer-native)

set(CMAKE_CXX_FLAGS "$CMAKE_CXX_FLAGS -std=c++11")

FIND_PACKAGE(Java REQUIRED)
FIND_PACKAGE(JNI REQUIRED)
INCLUDE_DIRECTORIES($JAVA_INCLUDE_PATH)
INCLUDE_DIRECTORIES($JAVA_INCLUDE_PATH2)

file(GLOB SOURCE_FILES "src/sources/*.c" "src/sources/*.h")

add_executable(ArgentumTracer-native $SOURCE_FILES)
add_library(argentumtracer SHARED $SOURCE_FILES)

target_link_libraries(ArgentumTracer-native protobuf-c)

请帮我建立库。我的仓库放在 github repository 分支 develop

【问题讨论】:

您至少需要分享您的 CMake 代码。乍一看,您似乎没有正确找到/链接 protobuf 库。 我将完整的源代码放在存储库中。 【参考方案1】:

正如我在评论中所建议的,您需要使用 CMake 来查找 protobuf 并将 protobuf 库添加到您的 target_link_libraries。你确实有库,protobuf-c,在链接中明确说明,但这并不是一个万无一失的方法。可以肯定的是,您应该尝试以下...。

find_package(Protobuf MODULE)
if(NOT PROTOBUF_FOUND)
  message(FATAL_ERROR "-- Protobuf is required!")
endif()

target_link_libraries(ArgentumTracer-native $PROTOBUF_LIBRARIES)

现在,您可以确定您拥有正确的库。此外,如果找不到 protobuf,您将知道 find_package 何时失败并有机会为 CMake 提供真实位置。

【讨论】:

以上是关于Cmake错误构建库的主要内容,如果未能解决你的问题,请参考以下文章

text 用于构建库和测试套件的有用cmake

CMake 找不到 SDL2

C# cmake 构建中的 XAML 文件冲突

在 Windows 上使用 CMake 构建 open62541 失败

yaml-cpp 未定义的 CMake 引用

如何使用 Visual Studio 编译器使用 cgo 构建库?