0

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.

6
  • "I have peace of code what I need to run with root, so I need to run it using g++" - Why would you need to compile the code as root in order to run it as root? Commented Jun 29, 2016 at 13:58
  • @JesperJuhl How do you mean run as root? I don't want to set privileges to project folder and etc.. I just want run code using g++, test it and then maybe make script or something to run it automatically. Commented Jun 29, 2016 at 14:03
  • One does not "run" code with g++. g++ is a compiler (probably the same compiler used by CLion). Compilers compile code and generate executables (programs) that you then run (execute). The compiler doesn't run the program. Commented Jun 29, 2016 at 14:07
  • @JesperJuhl ook, so if I understand right, I don't need to compile my code using g++ again. I need executable file from CLion what I will run with root permissions? Commented Jun 29, 2016 at 14:16
  • @JesperJuhl I don't even know how to make that file.. That's why I want to compile with g++ and make it (as one of possible solution).. You know how to do it? I understand that I need executable file to run it with root... But I need to create it before.. Commented Jun 29, 2016 at 14:23

1 Answer 1

1

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
# 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.