我正在尝试使用CMake从PubNub C++ git repository编译样本,以将其包含在我的一个项目中。
作为测试,我选择编译名为的样本 来自provided Makefile的 cancel_subscribe_sync_sample 。
使用Makefile,它编译时没有任何错误。
这是我提出的CMakeLists(我不是CMake的专家):
cmake_minimum_required(VERSION 3.4)
project(untitled)
set(CMAKE_CXX_FLAGS "-Wall -std=c++11")
set(SOURCE_FILES cancel_subscribe_sync_sample.cpp)
set(PUBNUB_FILES c-core/core/pubnub_coreapi.c
c-core/core/pubnub_coreapi_ex.c
c-core/core/pubnub_ccore.c
c-core/core/pubnub_netcore.c
c-core/lib/sockets/pbpal_sockets.c
c-core/lib/sockets/pbpal_resolv_and_connect_sockets.c
c-core/core/pubnub_alloc_std.c
c-core/core/pubnub_assert_std.c
c-core/core/pubnub_generate_uuid.c
c-core/core/pubnub_blocking_io.c
c-core/core/pubnub_timers.c
c-core/core/pubnub_json_parse.c
c-core/core/pubnub_helper.c
c-core/posix/pubnub_version_posix.c
c-core/posix/pubnub_generate_uuid_posix.c
c-core/posix/pbpal_posix_blocking_io.c
c-core/posix/monotonic_clock_get_time_posix.c)
include_directories(c-core/posix)
include_directories(c-core/cpp)
include_directories(c-core/core)
add_definitions(-DPUBNUB_THREADSAFE)
add_executable(untitled ${SOURCE_FILES} c-core/core/pubnub_ntf_sync.c c-core/cpp/pubnub_futres_sync.cpp ${PUBNUB_FILES})
link_libraries(untitled pthread rt)
在Ubuntu上将它与CLion一起使用,编译失败:
/home/barrauh/clion-2016.1/bin/cmake/bin/cmake --build /home/barrauh/.CLion2016.1/system/cmake/generated/untitled-609ca616/609ca616/Debug --target untitled -- -j 4
[ 4%] Building CXX object CMakeFiles/untitled.dir/cancel_subscribe_sync_sample.cpp.o
[ 9%] Building C object CMakeFiles/untitled.dir/c-core/core/pubnub_ntf_sync.c.o
[ 14%] Building CXX object CMakeFiles/untitled.dir/c-core/cpp/pubnub_futres_sync.cpp.o
[ 19%] Building C object CMakeFiles/untitled.dir/c-core/core/pubnub_coreapi.c.o
[ 23%] Building C object CMakeFiles/untitled.dir/c-core/core/pubnub_coreapi_ex.c.o
[ 28%] Building C object CMakeFiles/untitled.dir/c-core/core/pubnub_ccore.c.o
[ 33%] Building C object CMakeFiles/untitled.dir/c-core/core/pubnub_netcore.c.o
[ 38%] Building C object CMakeFiles/untitled.dir/c-core/lib/sockets/pbpal_sockets.c.o
[ 42%] Building C object CMakeFiles/untitled.dir/c-core/lib/sockets/pbpal_resolv_and_connect_sockets.c.o
[ 47%] Building C object CMakeFiles/untitled.dir/c-core/core/pubnub_alloc_std.c.o
[ 52%] Building C object CMakeFiles/untitled.dir/c-core/core/pubnub_assert_std.c.o
[ 57%] Building C object CMakeFiles/untitled.dir/c-core/core/pubnub_generate_uuid.c.o
[ 61%] Building C object CMakeFiles/untitled.dir/c-core/core/pubnub_blocking_io.c.o
[ 66%] Building C object CMakeFiles/untitled.dir/c-core/core/pubnub_timers.c.o
[ 71%] Building C object CMakeFiles/untitled.dir/c-core/core/pubnub_json_parse.c.o
[ 76%] Building C object CMakeFiles/untitled.dir/c-core/core/pubnub_helper.c.o
[ 80%] Building C object CMakeFiles/untitled.dir/c-core/posix/pubnub_version_posix.c.o
[ 85%] Building C object CMakeFiles/untitled.dir/c-core/posix/pubnub_generate_uuid_posix.c.o
[ 90%] Building C object CMakeFiles/untitled.dir/c-core/posix/pbpal_posix_blocking_io.c.o
[ 95%] Building C object CMakeFiles/untitled.dir/c-core/posix/monotonic_clock_get_time_posix.c.o
[100%] Linking CXX executable untitled
CMakeFiles/untitled.dir/cancel_subscribe_sync_sample.cpp.o: In function `pubnub::context::context(std::string, std::string)':
/home/barrauh/ClionProjects/untitled/c-core/cpp/pubnub_common.hpp:201: undefined reference to `pubnub_alloc()'
/home/barrauh/ClionProjects/untitled/c-core/cpp/pubnub_common.hpp:205: undefined reference to `pubnub_init(pubnub_*, char const*, char const*)'
CMakeFiles/untitled.dir/cancel_subscribe_sync_sample.cpp.o: In function `pubnub::context::get() const':
/home/barrauh/ClionProjects/untitled/c-core/cpp/pubnub_common.hpp:267: undefined reference to `pubnub_get(pubnub_*)'
CMakeFiles/untitled.dir/cancel_subscribe_sync_sample.cpp.o: In function `pubnub::context::cancel()':
/home/barrauh/ClionProjects/untitled/c-core/cpp/pubnub_common.hpp:305: undefined reference to `pubnub_cancel(pubnub_*)'
CMakeFiles/untitled.dir/cancel_subscribe_sync_sample.cpp.o: In function `pubnub::context::subscribe(std::string const&, std::string const&)':
/home/barrauh/ClionProjects/untitled/c-core/cpp/pubnub_common.hpp:328: undefined reference to `pubnub_subscribe(pubnub_*, char const*, char const*)'
CMakeFiles/untitled.dir/cancel_subscribe_sync_sample.cpp.o: In function `pubnub::context::set_blocking_io(pubnub::blocking_io)':
/home/barrauh/ClionProjects/untitled/c-core/cpp/pubnub_common.hpp:537: undefined reference to `pubnub_set_blocking_io(pubnub_*)'
/home/barrauh/ClionProjects/untitled/c-core/cpp/pubnub_common.hpp:540: undefined reference to `pubnub_set_non_blocking_io(pubnub_*)'
CMakeFiles/untitled.dir/cancel_subscribe_sync_sample.cpp.o: In function `pubnub::context::~context()':
/home/barrauh/ClionProjects/untitled/c-core/cpp/pubnub_common.hpp:580: undefined reference to `pubnub_free(pubnub_*)'
CMakeFiles/untitled.dir/c-core/cpp/pubnub_futres_sync.cpp.o: In function `pubnub::futres::last_result()':
/home/barrauh/ClionProjects/untitled/c-core/cpp/pubnub_futres_sync.cpp:34: undefined reference to `pubnub_last_result(pubnub_*)'
CMakeFiles/untitled.dir/c-core/cpp/pubnub_futres_sync.cpp.o: In function `pubnub::futres::end_await()':
/home/barrauh/ClionProjects/untitled/c-core/cpp/pubnub_futres_sync.cpp:50: undefined reference to `pubnub_await(pubnub_*)'
CMakeFiles/untitled.dir/c-core/core/pubnub_coreapi.c.o: In function `pubnub_init':
/home/barrauh/ClionProjects/untitled/c-core/core/pubnub_coreapi.c:20: undefined reference to `pthread_mutexattr_init'
/home/barrauh/ClionProjects/untitled/c-core/core/pubnub_coreapi.c:20: undefined reference to `pthread_mutexattr_settype'
collect2: error: ld returned 1 exit status
make[3]: *** [untitled] Error 1
make[2]: *** [CMakeFiles/untitled.dir/all] Error 2
make[1]: *** [CMakeFiles/untitled.dir/rule] Error 2
make: *** [untitled] Error 2titled/c-core/cpp/pubnub_common.hpp:580: undefined reference to `pubnub_free(pubnub_*)'
CMakeFiles/untitled.dir/c-core/cpp/pubnub_futres_sync.cpp.o: In function `pubnub::futres::last_result()':
/home/barrauh/ClionProjects/untitled/c-core/cpp/pubnub_futres_sync.cpp:34: undefined reference to `pubnub_last_result(pubnub_*)'
CMakeFiles/untitled.dir/c-core/cpp/pubnub_futres_sync.cpp.o: In function `pubnub::futres::end_await()':
/home/barrauh/ClionProjects/untitled/c-core/cpp/pubnub_futres_sync.cpp:50: undefined reference to `pubnub_await(pubnub_*)'
CMakeFiles/untitled.dir/c-core/core/pubnub_coreapi.c.o: In function `pubnub_init':
/home/barrauh/ClionProjects/untitled/c-core/core/pubnub_coreapi.c:20: undefined reference to `pthread_mutexattr_init'
/home/barrauh/ClionProjects/untitled/c-core/core/pubnub_coreapi.c:20: undefined reference to `pthread_mutexattr_settype'
collect2: error: ld returned 1 exit status
make[3]: *** [untitled] Error 1
make[2]: *** [CMakeFiles/untitled.dir/all] Error 2
make[1]: *** [CMakeFiles/untitled.dir/rule] Error 2
make: *** [untitled] Error 2
我真的不明白为什么。例如,函数 pubnub_alloc()在文件 pubnub_alloc_std.c 中定义。
这让我疯了......我在这里做错了吗?
谢谢!
答案 0 :(得分:2)
我的猜测是,'pubnub_alloc_std.c'文件是用C编译器编译的(我猜gcc
)。 C使用与C ++不同的name mangling,因此需要告诉C ++编译器这些函数使用C名称修改。一个人通常使用extern "C"
说明符。
然而,这似乎在这个文件中被注释掉了: https://github.com/pubnub/c-core/blob/master/cpp/pubnub_common.hpp(第5-14行)
两种可能的解决方案:
extern "C"
部分
-DCMAKE_C_COMILER=g++
答案 1 :(得分:0)
set(CMAKE_CXX_FLAGS“ -Wall -std = c ++ 11 -D PUBNUB_USE_EXTERN_C = 1”)