【发布时间】:2016-06-29 13:49:07
【问题描述】:
我正在尝试RabbitMQ + C++。在 linux ubuntu 16.04 上工作。有工作代码,当我使用CLion 编译时一切正常。
我不需要使用 root 运行所需的代码,所以我想使用 g++ 运行它。
来自终端的错误
In function `main':
receiveUNPW.cpp:(.text+0x8e8): undefined reference to `SimplePocoHandler::SimplePocoHandler(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned short)'
receiveUNPW.cpp:(.text+0xc9c): undefined reference to `SimplePocoHandler::loop()'
receiveUNPW.cpp:(.text+0xcce): undefined reference to `SimplePocoHandler::~SimplePocoHandler()'
receiveUNPW.cpp:(.text+0xea7): undefined reference to `SimplePocoHandler::~SimplePocoHandler()'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libamqpcpp.so: undefined reference to `pthread_create'
我写:
g++ -std=c++11 receiveUNPW.cpp -o receiveUNPW -lcrypt -lPocoNet -lPocoFoundation -lamqpcpp
CMakeList.txt
cmake_minimum_required(VERSION 3.5)
project(test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_library(poco_simple_handler SimplePocoHandler.cpp SimplePocoHandler.h)
target_link_libraries(poco_simple_handler PocoNet PocoFoundation crypt )
set(PROGS
sendUNPW
receiveUNPW
)
foreach(item ${PROGS})
add_executable(${item} "${item}.cpp")
target_link_libraries(${item} amqpcpp poco_simple_handler PocoNet PocoFoundation crypt)
endforeach(item)
我的想法
当我使用g++ 时可以看到它找不到对SimplePocoHandler 的引用。在CmakeList.txt 我有
add_library(poco_simple_handler SimplePocoHandler.cpp SimplePocoHandler.h)
target_link_libraries(poco_simple_handler PocoNet PocoFoundation crypt )
所以当我在 CLion 中编译时一切正常。所以当我使用g++ 时,我似乎也需要这样做。但我不知道该怎么做,任何建议或解释都会很棒。
我不分享我的receiveUNPW.cpp 代码,但它几乎与receiver.cpp 相似,你可以看到there,而且我没有任何错误,在CLion 中一切正常,我只需要使用具有 root 权限的终端运行我的程序。
【问题讨论】:
-
“我不需要使用 root 运行的代码,所以我需要使用 g++ 运行它” - 为什么需要以 root 身份编译代码才能以 root 身份运行?
-
@JesperJuhl 你是什么意思以 root 身份运行?我不想为项目文件夹等设置权限。我只想使用 g++ 运行代码,对其进行测试,然后可能会制作脚本或其他东西来自动运行它。
-
不能用 g++“运行”代码。 g++ 是一个编译器(可能与 CLion 使用的编译器相同)。编译器编译代码并生成可执行文件(程序),然后您运行(执行)。编译器不运行程序。
-
@JesperJuhl 好的,所以如果我理解正确,我不需要再次使用 g++ 编译我的代码。我需要来自 CLion 的可执行文件,我将以 root 权限运行什么?
-
@JesperJuhl 我什至不知道如何制作那个文件。这就是为什么我想用
g++编译并制作它(作为一种可能的解决方案)。你知道该怎么做它?我知道我需要可执行文件才能使用 root 运行它...但我需要先创建它..