【问题标题】:Building PubNub C++ with CMake使用 CMake 构建 PubNub C++
【发布时间】:2023-04-10 01:17:01
【问题描述】:

我正在尝试使用 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 中。

这让我发疯了......我在这里做错了吗?

谢谢!

【问题讨论】:

    标签: c++ cmake pubnub clion


    【解决方案1】:

    我的猜测是,'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 行)

    两种可能的解决方案:

    1. 尝试在 extern "C" 部分评论回来
    2. 使用 C++ 编译器编译 c 文件。您可以通过使用标志 -DCMAKE_C_COMILER=g++ 运行 cmake 来做到这一点

    【讨论】:

    • 现在运行良好。我取消了一些extern "C" 的注释,瞧!我不知道这个说明符存在。谢谢!
    【解决方案2】:

    设置(CMAKE_CXX_FLAGS "-Wall -std=c++11 -D PUBNUB_USE_EXTERN_C=1")

    【讨论】:

      猜你喜欢
      • 2020-05-27
      • 2020-12-15
      • 2018-03-26
      • 1970-01-01
      • 2017-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-26
      相关资源
      最近更新 更多