2016-05-15 81 views
1

我試圖從使用CMake的PubNub C++ git repository中編譯一個樣本,將它包含在我的一個項目中。使用CMake構建PubNub C++

作爲測試,我選擇編譯從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克利翁使用它,編譯失敗:

/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_std.c中定義了函數pubnub_alloc()

這使我瘋狂......我在這裏做錯了什麼?

謝謝!

回答

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)

兩個可能的解決方案:

  1. 嘗試
  2. 編譯評論的extern "C"部分回c文件使用C++編譯器。你可以通過運行帶有標誌的cmake來達到這個目的-DCMAKE_C_COMILER=g++
+0

現在它工作得很好。我取消了一些「extern」C「'和voila! 我不知道這個說明符存在。 謝謝! –