使用 CMake 构建 PubNub C++
Building PubNub C++ with CMake
我正在尝试使用 CMake 从 PubNub C++ git repository 编译一个示例,以将其包含在我的一个项目中。
作为测试,我选择编译名为
cancel_subscribe_sync_sample 来自 provided Makefile.
使用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.
中定义
快把我逼疯了...我是不是做错了什么?
谢谢!
我猜 '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"
部分
- 使用 C++ 编译器编译 c 文件。您可以通过 运行 带有标志
-DCMAKE_C_COMILER=g++
的 cmake 来做到这一点
set(CMAKE_CXX_FLAGS "-Wall -std=c++11 -D PUBNUB_USE_EXTERN_C=1")
我正在尝试使用 CMake 从 PubNub C++ git repository 编译一个示例,以将其包含在我的一个项目中。
作为测试,我选择编译名为 cancel_subscribe_sync_sample 来自 provided Makefile.
使用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.
中定义快把我逼疯了...我是不是做错了什么?
谢谢!
我猜 '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 行)
两种可能的解决方案:
- 尝试在 中评论
- 使用 C++ 编译器编译 c 文件。您可以通过 运行 带有标志
-DCMAKE_C_COMILER=g++
的 cmake 来做到这一点
extern "C"
部分
set(CMAKE_CXX_FLAGS "-Wall -std=c++11 -D PUBNUB_USE_EXTERN_C=1")