简体   繁体   English

使用g ++在终端中运行.cpp文件时出错

[英]error when runing .cpp file in terminal using g++

I trying RabbitMQ + C++ . 我正在尝试RabbitMQ + C++ Working on linux ubuntu 16.04. 在Linux Ubuntu 16.04上工作。 Have working code and when I compile using CLion all works fine. 有工作代码,当我使用CLion编译时,一切正常。 I have peace of code what I need to run with root, so I want to run it using g++ . 我拥有使用root运行的代码,因此我想使用g++运行它。

ERROR FROM TERMINAL 终端错误

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'

I write : 我写 :

g++ -std=c++11 receiveUNPW.cpp -o receiveUNPW -lcrypt -lPocoNet -lPocoFoundation -lamqpcpp 

CMakeList.txt 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)

MY IDEA 我的点子

As can see when I use g++ it can't find reference to SimplePocoHandler . 可以看出,当我使用g++ ,找不到对SimplePocoHandler引用。 In CmakeList.txt i have CmakeList.txt我有

add_library(poco_simple_handler SimplePocoHandler.cpp SimplePocoHandler.h)
target_link_libraries(poco_simple_handler PocoNet PocoFoundation crypt )

so when I compile in CLion all works fine. 因此,当我在CLion中编译时,一切正常。 So seems I need to do the same when I using g++ . 所以看起来我在使用g++时也需要做同样的事情。 But I don't know how to do it, any suggestions or explanations would be great. 但是我不知道该怎么做,任何建议或解释都很好。

I don't share my receiveUNPW.cpp code, but it's almost similar to that receiver.cpp what you can see there and also I don't have any error there, in CLion all works fine, I just need to run my program using terminal with root permissions. 我不共享我的receiveUNPW.cpp代码,但是它几乎与该receiver.cpp类似,在那里您可以看到而且我也没有任何错误,在CLion一切正常,我只需要使用具有root权限的终端。

To run your code as root, change to a root shell using su - and then execute the binary that CLion generated. 要运行您的代码为根,使用更改为root的shell su -然后执行该克利翁生成的二进制。 Or run it using sudo . 或者使用sudo运行它。 You can also set the suid bit on the executable and make it be owned by root then it will always run as root, but that's not recommended - too many security issues possible. 您还可以在可执行文件上设置suid位,并使其由root拥有,然后它将始终以root身份运行,但是不建议这样做-可能存在太多安全问题。

You don't need to recompile an application as root to run it as root. 您无需以root用户身份重新编译应用程序即可以root用户身份运行该应用程序。

Edit with example as requested: 根据要求编辑示例:

A simple program: 一个简单的程序:

#include <iostream>
int main() {
    std::cout << "Hello world\n";
}

Compiling it: 编译:

$ g++ hello.cc

Running it: 运行它:

$ ./a.out
Hello world
$ 

Running it as root: 以root身份运行:

$ su -
# /path/to/program/a.out
Hello world
# 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM