Trimidas
Trimidas

Reputation: 97

error when runing .cpp file in terminal using g++

I trying RabbitMQ + C++. Working on linux ubuntu 16.04. Have working code and when I compile using CLion all works fine. I have peace of code what I need to run with root, so I want to run it using 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

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. In CmakeList.txt i have

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. So seems I need to do the same when I using 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.

Upvotes: 0

Views: 669

Answers (1)

Jesper Juhl
Jesper Juhl

Reputation: 31447

To run your code as root, change to a root shell using su - and then execute the binary that CLion generated. Or run it using 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.

You don't need to recompile an application as root to run it as 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:

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

Upvotes: 1

Related Questions