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

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.

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
#

Related

Linking Paho C Mqtt library error in C++ Project

I'm trying to include the MQTT-C-Client-Library in a simple C++ project.
I have included the header file succesfully like this #include "MQTTClient.h". Compiling it in the linux terminal was printing this errors:
[xy#localhost mosquittoProject]$ sudo g++ *.cpp -o MQTTTest
/tmp/ccHn3s6m.o: In function `main':
mosquitto_test.cpp:(.text+0x11e): undefined reference to `MQTTClient_create'
mosquitto_test.cpp:(.text+0x13f): undefined reference to `MQTTClient_connect'
collect2: error: ld returned 1 exit status
I figured out that I need to link the library after some googling: Example MQTT Client Code not working C
Based on this question and answer I tried compiling it again like this:
sudo g++ -L/home/xy/Desktop/paho.mqtt.c/build/output/ *.cpp -l paho-mqtt3c -o MQTTTest
Which compiles fine but when running I get still an error.
Console commands and output:
[xy#localhost mosquittoProject]$ sudo g++ -L/home/xy/Desktop/paho.mqtt.c/build/output/ *.cpp -l paho-mqtt3c -o MQTTTest
[xy#localhost mosquittoProject]$ ./MQTTTest
./MQTTTest: error while loading shared libraries: libpaho-mqtt3c.so.1: cannot open shared object file: No such file or directory
I replaced the actual username by xy in this post.
What am I doing wrong here?
The problem looks like the library (libpaho-mqtt3c.so.1) is not on the library path.
It looks like you are linking against the build location of the library and have not installed it to the default system location (e.g. /usr/local/lib) by running sudo make install.
By default on Linux the runtime linker searches the locations listed in /etc/ld.so.conf and /etc/ld.so.conf.d. if you edit these remember to run sudo ldconfig to update the cache.
You can add the location of the library to the LD_LIBRARY_PATH environment variable e.g.:
$ LD_LIBRARY_PATH=/home/xy/Desktop/paho.mqtt.c/build/output/ ./MQTTTest

Fresh installation of bullet physics engine is not linking properly?

Have a relatively new and fresh installation of ubuntu LTS, I followed the instructions found here for generic dynamic library building: http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Installation
Installation appears to go smoothly (no errors or warnings) and afterwards I have all of the necessary files located in /usr/local/include/bullet. I'm currently attempting to compile a file from the following shell command:
g++ bulletTest.cpp -lGL -lGLU -I /usr/local/include/bullet/
bulletTest.cpp is:
#include <iostream>
#include <btBulletDynamicsCommon.h>
int main (void)
{
btBroadphaseInterface* broadphase;
return 0;
}
This file has nothing to it, but is throwing a plethora of undefined reference errors, such as "/tmp/ccH09wLR.o:(.rodata._ZTV17btTypedConstraint[_ZTV17btTypedConstraint]+0x30): undefined reference to `__cxa_pure_virtual'" I cannot find any other complaints on the forums/SO so I believe it must be a problem with my build/installation. These types of errors occur while attempting both gcc and g++ compilers.
Anybody have an idea?
EDIT: changed my shell command to g++ bulletTest.cpp -lGL -lGLU -lBulletDynamics -lBulletCollision -lBulletSoftBody -lLinearMath -I /usr/local/include/bullet/
It compiles but does not run, error is: error while loading shared libraries: libBulletDynamics.so.2.82: cannot open shared object file: No such file or directory
So it was a lot of little stuff. Firstly, don't reference "lbullet" reference each of the libraries needed:
g++ bulletTest.cpp -o bulletTest -lGL -lGLU -lBulletDynamics -lBulletCollision -lBulletSoftBody -lLinearMath -I /usr/local/include/bullet/
In addition, I had to set the LD_LIBRARY_PATH variable
export LD_LIBRARY_PATH=/usr/local/lib
and then it would both compile and run.

Undefined reference to boost::random::random_device constructor and destructor on MinGW-w64 gcc

My OS is Windows 7 64-bit and C++ compiler I'm using is:
g++ (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 5.3.0
And I installed Boost version 1.60 using:
bootstrap.bat mingw
b2 install target=gcc
Then I tested is it working, using examples from Boost.Random tutorial.
With the first two everything was fine, but the third one gave linker errors about boost::random::random_device. I minimized the code to have only this:
// Compiled with:
// g++ -IC:/Boost/include/boost-1_60
// -LC:/Boost/lib -lboost_random-mgw53-mt-1_60
// main.cpp
#include "boost/random/random_device.hpp"
int main() {
boost::random::random_device rng;
}
And I get the following errors:
C:\Users\Daniel\AppData\Local\Temp\cc5DfdjZ.o:main.cpp:(.text+0x15):
undefined reference to `boost::random::random_device::random_device()'
C:\Users\Daniel\AppData\Local\Temp\cc5DfdjZ.o:main.cpp:(.text+0x20):
undefined reference to `boost::random::random_device::~random_device()'
collect2.exe: error: ld returned 1 exit status
Here, on SO, I found that someone with similar problem added -lboost_system to flags, but for me it didn't helped.
Does anyone have any idea, why it isn't working? I checked, and I have random_device.hpp header in my Boost folder, with declarations of random_device() and ~random_device() in it.
I found what was wrong - the g++ command syntax, that I wanted to use to compile and link my code.
As I wrote in my question, I do this that way:
g++ -IC:/Boost/include/boost-1_60 -LC:/Boost/lib -lboost_random-mgw53-mt-1_60 main.cpp
While the correct one is with main.cpp (or any other source code file(s), that we want to include in compiling process) before the -L and -l flags.
For example:
g++ -IC:/Boost/include/boost-1_60 main.cpp -LC:/Boost/lib -lboost_random-mgw53-mt-1_60
or even
g++ main.cpp -IC:/Boost/include/boost-1_60 -LC:/Boost/lib -lboost_random-mgw53-mt-1_60
Hope it will help anyone, who will make such silly mistake too.

When I use Cmake to compile the file, it shows /usr/bin/ld: cannot find -ltest. However when I use g++ to compile the file, it is ok

This is the CMakeList.txt in src directory:
ADD_EXECUTABLE(Expression ExpressionValue.cpp)
INCLUDE_DIRECTORIES(/root/fengwu/codes/headfiles/expression/include)
LINK_DIRECTORIES(/root/fengwu/codes/headfiles/expression/lib)
TARGET_LINK_LIBRARIES(Expression test)
When I enter cmake .. in build directory, it is ok. After that I enter make, it shows /usr/bin/ld: cannot find -ltest. However when I use g++, it is ok.
g++ ExpressionValue.cpp
-I/root/fengwu/codes/headfiles/expression/include -L/root/fengwu/codes/headfiles/expression/lib -ltest -o expr.out
From the documentation for link_directories:
The command will apply only to targets created after it is called.
For make link_directories() call work you need to move it before add_executable().

How to compile a c++ program in Linux?

I made a file hi.cpp and I wrote the command given below:
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! ";
cout << "I'm a C++ program";
return 0;
}
then I ran it in my RHEL 6 machine with the following command
gcc hi.cpp
and I got some errors which are as follows:
[chankey#localhost ~]$ gcc hi.cpp
/tmp/cc32bnmR.o: In function `main':
hi.cpp:(.text+0xa): undefined reference to `std::cout'
hi.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, const char*)'
hi.cpp:(.text+0x19): undefined reference to `std::cout'
hi.cpp:(.text+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, const char*)'
/tmp/cc32bnmR.o: In function `__static_initialization_and_destruction_0(int, int)':
hi.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::Init()'
hi.cpp:(.text+0x51): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cc32bnmR.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
[chankey#localhost ~]$
What do these errors denote? My code is correct then why am I getting errors?
Use g++
g++ -o hi hi.cpp
g++ is for C++, gcc is for C although with the -libstdc++ you can compile c++ most people don't do this.
As the other answers say, use g++ instead of gcc.
Or use make: make hi
You have to use g++ (as mentioned in other answers). On top of that you can think of providing some good options available at command line (which helps you avoid making ill formed code):
g++ -O4 -Wall hi.cpp -o hi.out
^^^^^ ^^^^^^
optimize related to coding mistakes
For more detail you can refer to man g++ | less.
Try this:
g++ -o hi hi.cpp
gcc is only for C
For a simple hello-world project, calling the compiler directly with g++ command or creating a make file are good options as already answered:
g++ -o hi hi.cpp
or
# After creating the makefile
make hi
For serious projects, however, the usage of a project manager is required. At the time I write this answer, the most used and open-source is cmake (an alternative could be QT qmake ).
Following is a simple CMake example:
Make sure you installed cmake on your linux distribution apt-get install cmake or yum install cmake.
Create a file CMakeLists.txt (the name is important) together with your source hi.cpp
project("hi")
add_executable( hi hi.cpp )
Then compile and run as:
cmake -B <path_to_build_folder> -S <path_to_source_folder>
cmake --build <path_to_build_folder>
cd <path_to_build_folder>; ./hi
This allows the project to scale easily with libraries, sources, unit-tests, and much more. It also makes most IDEs to understand the project properly (Most IDEs accept CMake natively, like kdevelop, qtCreator, etc..)
You could also generate Visual-Studio or XCode projects from CMake, in case you decide to port the software to other platforms in the future.
cmake -G Xcode . #will generate `hi.xcodeproj` you can load on macOS
$ g++ 1st.cpp -o 1st
$ ./1st
if you found any error then first install g++ using code as below
$ sudo apt-get install g++
then install g++ and use above run code
g++ -o foo foo.cpp
g++ --> Driver for cc1plus compiler
-o --> Indicates the output file (foo is the name of output file here. Can be any name)
foo.cpp --> Source file to be compiled
To execute the compiled file simply type
./foo
To Compile your C++ code use:-
g++ file_name.cpp -o executable_file_name
(i) -o option is used to show error in the code
(ii) if there is no error in the code_file, then it will generate
an executable file.
Now execute the generated executable file:
./executable_file_name