poco cmake文件和本地库

时间:2016-03-22 08:57:14

标签: c++ linux linker cmake raspberry-pi2

首先,我是Linux新手,对C ++没有太多经验,这就是我需要你帮助的原因。

基本上,我有一个Raspberry Pi,带有最新的RASPBIAN JESSIE图像,我必须在其上实现一个websockets服务器并集成一些C ++ CAN库。

对于服务器,我选择使用POCO C ++库。我设法建立了一个服务器,但现在我必须包含CAN和ArduPi库(CAN库不是我刚收到的用于修复一些错误并将其集成到服务器实现中的创建)。为了构建和编译服务器,我使用了一个CMakeList文件,如下所示:link

CMakeList.txt看起来像:

#Ref http://stackoverflow.com/questions/30114662/clion-cmake-and-poco
cmake_minimum_required(VERSION 3.3)
project(PoCoWebSocketTest)
# define the project
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lrt -lpthread")
set(SOURCE_FILES main.cpp)
add_executable(PoCoWebSocketTest ${SOURCE_FILES})
# set the POCO paths and libs
set(POCO_PREFIX "/usr/local") # the directory containing "include" and "lib"
set(POCO_INCLUDE_DIR"${POCO_PREFIX}/include")
set(POCO_LIB_DIR "${POCO_PREFIX}/lib")
set(POCO_LIBS
        "${POCO_LIB_DIR}/libPocoNet.so"
        "${POCO_LIB_DIR}/libPocoUtil.so"
        "${POCO_LIB_DIR}/libPocoFoundation.so")
# set the include path for the app
target_include_directories(PoCoWebSocketTest PRIVATE $(POCO_INCLUDE_DIR))
# link the app against POCO
target_link_libraries(PoCoWebSocketTest "${POCO_LIBS}")

执行make命令后的输出:

    pi@raspberrypi:~/pocotests/build $ sudo make
[ 50%] Building CXX object CMakeFiles/PoCoWebSocketTest.dir/main.cpp.o
[100%] Linking CXX executable PoCoWebSocketTest
CMakeFiles/PoCoWebSocketTest.dir/main.cpp.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x154): undefined reference to `CAN::CAN()'
collect2: error: ld returned 1 exit status
CMakeFiles/PoCoWebSocketTest.dir/build.make:97: recipe for target 'PoCoWebSocketTest' failed
make[2]: *** [PoCoWebSocketTest] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/PoCoWebSocketTest.dir/all' failed
make[1]: *** [CMakeFiles/PoCoWebSocketTest.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

主文件的include部分是:

#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Net/HTTPRequestHandlerFactory.h"
#include "Poco/Net/HTTPServerParams.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "Poco/Net/HTTPServerParams.h"
#include "Poco/Net/ServerSocket.h"
#include "Poco/Net/WebSocket.h"
#include "Poco/Net/NetException.h"
#include "Poco/Util/ServerApplication.h"
#include "Poco/Util/Option.h"
#include "Poco/Util/OptionSet.h"
#include "Poco/Util/HelpFormatter.h"
#include "Poco/Format.h"
#include <iostream>
#include "arduPi.h"
#include "CAN.h"

using Poco::Net::ServerSocket;
using Poco::Net::WebSocket;
using Poco::Net::WebSocketException;
using Poco::Net::HTTPRequestHandler;
using Poco::Net::HTTPRequestHandlerFactory;
using Poco::Net::HTTPServer;
using Poco::Net::HTTPServerRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPServerResponse;
using Poco::Net::HTTPServerParams;
using Poco::Timestamp;
using Poco::ThreadPool;
using Poco::Util::ServerApplication;
using Poco::Util::Application;
using Poco::Util::Option;
using Poco::Util::OptionSet;
using Poco::Util::HelpFormatter;

据我所知,链接可能存在一些问题,但我不确定。我应该在cmakefile中添加CAN和ArduPi库吗?

如果您需要有关代码的更多信息,请告诉我。

致以最诚挚的问候,

1 个答案:

答案 0 :(得分:1)

问题解决了,我通过添加CAN和arduPi库并将它们链接到主文件来修改make文件。

这就是文件的样子:

#Ref http://stackoverflow.com/questions/30114662/clion-cmake-and-poco
cmake_minimum_required(VERSION 3.3)
project(WebSocketServerCPP)
# define the project
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lrt -lpthread")
set(SOURCE_FILES main.cpp)
add_executable(WebSocketServerCPP ${SOURCE_FILES})
# set the POCO paths and libs
set(POCO_PREFIX "/usr/local") # the directory containing "include" and "lib"
set(POCO_INCLUDE_DIR"${POCO_PREFIX}/include")
set(POCO_LIB_DIR "${POCO_PREFIX}/lib")
set(POCO_LIBS
        "${POCO_LIB_DIR}/libPocoNet.so"
        "${POCO_LIB_DIR}/libPocoUtil.so"
        "${POCO_LIB_DIR}/libPocoFoundation.so")
add_library(libcan STATIC CAN.cpp)
add_library(libardupi STATIC arduPi.cpp)
# set the include path for the app
target_include_directories(WebSocketServerCPP PRIVATE $(POCO_INCLUDE_DIR))
# link the app against POCO
target_link_libraries(libcan libardupi)
target_link_libraries(WebSocketServerCPP libcan)
target_link_libraries(WebSocketServerCPP libardupi)
target_link_libraries(WebSocketServerCPP "${POCO_LIBS}")

谢谢Tsyvarev。

祝你好运, 马太