Compiling issues with boost - c++

I'm having problems with compiling a program which includes "boost/asio.hpp".
Compiling this program(taken from boost site):
example.cpp:
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
using namespace boost::lambda;
typedef std::istream_iterator<int> in;
std::for_each(
in(std::cin), in(), std::cout << (_1 * 3) << " " );
}
with
c++ -I path/to/boost_1_55_0 example.cpp -o example
works fine.
But when the program includes:
boost/asio.hpp
And I'm trying to compile it with:
g++ -I /usr/local/boost_1_55_0 example.cpp -o example -lboost_system -lboost_thread
an executable is generated ,but I'm getting this error when trying to execute "example":
./example: error while loading shared libraries: libboost_system.so.1.55.0: cannot open shared object file: No such file or directory
The file "libboost_system.so.1.55.0" is located at "/usr/local/lib".
I also tried to compile the program with :
g++ -I /usr/local/boost_1_55_0 -L/usr/local/lib example.cpp -o example -lboost_system -lboost_thread
And got the same error.
How can I fix this?

You need to tell the linker where to find the library it needs. I prefer RPATH for this:
g++ -I /usr/local/boost_1_55_0 -Wl,-rpath=/usr/local/lib example.cpp -o example -lboost_system -lboost_thread
That bakes /usr/local/lib into the executable so ld can find it later. You can see what ld will load by running ldd example after building. I bet right now it says "not found" and after adding RPATH it will find the library.
Another option is to set /usr/local/lib as a system search path in your /etc/ld.so.conf, but that's quite a bit more heavyweight.

set up LD_LIBRARY_PATH as export LD_LIBRARY_PATH= path to boost

Related

Problems including GLFW header in c++ program

I need to include GLFW header in my c++ program. The installation was fine and the GLFW folder exists in my usr\include folder and g++ does look for header files in that folder. Despite that it throws an error telling me that the GLFW directory doesnt exist.
I am using sublime text as my editor and IDE and my system is Ubuntu-20.04
FOllowing is the code, the terminal command i used to compile and the error message i encountered:
#include <GLFW\glfw3native.h>
#include <iostream>
int main(int argc, char const *argv[])
{
std::cout << "All DOne!!" << std::endl;
return 0;
}
g++ -g -o bin/debug/main src/*.cpp -x64 -std=c++17 -Wall -I -include -lglfw3 -lGL -lm -lXrandr -lXi -lX11 -lXxf86vm -lpthread && ./bin/debug.main
src/main.cpp:1:10: fatal error: GLFW\glfw3.h: No such file or directory
1 | #include <GLFW\glfw3.h>
| ^~~~~~~~~~~~~~
compilation terminated.
I cannot tell where the problem lies. please help.
and my system is Ubuntu-20.04
On Linux, the path delimiter is /, not \, so
#include <GLFW/glfw3native.h>
Note that while windows primarily uses backslash, it also accepts the forwad slash, so always using / is also the best option for cross-platform programming.

How to compile Google Protobuf command line interface compile

I am trying to raw decode a protobuf binary. I installed the google protobuf library from the source code https://github.com/google/protobuf
I am able to use the command line to decode raw a protobuf binary using the command protoc --decode_raw <encodedfile>. I want to be able to do this programmatically using the c++ library. Something similar to the following example in the documentation.
https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.compiler.command_line_interface
However trying to compile a simple piece of code does not work.
#include <iostream>
#include <fstream>
#include <string>
#include <google/protobuf/compiler/command_line_interface.h>
using namespace google::protobuf::compiler;
using namespace std;
int main(int argc, char* argv[]) {
google::protobuf::compiler::CommandLineInterface cli_;
cerr << "Compiled And Run" << endl;
}
Compile command
c++ my_program.cc -o my_program -pthread -I/usr/local/include -pthread -L/usr/local/lib -lprotobuf -lpthread
I see the following error
my_program.cc:(.text+0x24): undefined reference to `google::protobuf::compiler::CommandLineInterface::CommandLineInterface()'
my_program.cc:(.text+0x4f): undefined reference to `google::protobuf::compiler::CommandLineInterface::~CommandLineInterface()'
my_program.cc:(.text+0x70): undefined reference to `google::protobuf::compiler::CommandLineInterface::~CommandLineInterface()'
Appreciate any help with this.
Protobuf compiler is in a different library, libprotoc. You need to link against it
c++ my_program.cc -o my_program -pthread -I/usr/local/include -pthread -L/usr/local/lib -lprotoc -lprotobuf -lpthread
Note that -lprotoc needs to appear before -lprotobuf, because libprotoc uses functions of libprotobuf.

Compiling a program that is dependent on boost libraries with only .so or .a files

I have a small test case program just to see if boost works on some system.
#include <iostream>
#include <boost/math/distributions/normal.hpp>
#include <boost/math/distributions/chi_squared.hpp>
using namespace std;
int main(int argc, char* argv[]) {
// Boost test
boost::math::normal std_normal;
double x = 1.5;
cout << boost::math::cdf(std_normal, x) << endl;
boost::math::normal non_std_normal(1.5, 2);
cout << boost::math::cdf(non_std_normal, x) << endl; // should output 1/2
// Test the chi-squared inverse
int degree_of_freedom = 19;
boost::math::chi_squared chi_dist(degree_of_freedom);
cout << boost::math::quantile(complement(chi_dist, 0.05)) << endl;
return 0;
}
I ssh to some server and they only allow me to use boost libraries through some directory full of shared objects and archive files (i.e. libboost_log.so, libboost_math_c99.a, etc.).
To be honest, I have no clue how to use these files.
I tried (for both g++ and gcc)
g++ test.cpp -o test -l /share/apps/boost/1.55.0/lib
g++ test.cpp -o test -l /share/apps/boost/1.55.0/lib -lboost_system -lboost_filesystem
g++ -std=c++11 -pedantic test.cpp -I/share/apps/boost/1.55.0/include/ -o test
g++ test.cpp -o test -I /share/apps/boost/1.55.0/include -lboost_system -lboost_filesystem
where /share/apps/boost/1.55.0/lib is the directory for the .so and .a files
and /share/apps/boost/1.55.0/include is the directory for the .hpp files.
I was denied permission for the 3rd command with the following output:
In file included from
/share/apps/boost/1.55.0/include/boost/math/special_functions/detail/round_fwd.hpp:11:0,
from /share/apps/boost/1.55.0/include/boost/math/special_functions/math_fwd.hpp:26,
from /share/apps/boost/1.55.0/include/boost/math/special_functions/erf.hpp:13,
from /share/apps/boost/1.55.0/include/boost/math/distributions/normal.hpp:19,
from test.cpp:12: /share/apps/boost/1.55.0/include/boost/config.hpp:30:29: fatal error:
/share/apps/boost/1.56.0/build/boost_1_56_0/boost/config/user.hpp:
Permission denied
I received the error for the fourth command:
fatal error: boost/math/distributions/normal.hpp: No such file or
directory.
you want to use
g++ -isystem /share/apps/boost/1.55.0/include -L /share/apps/boost/1.55.0/lib test.cpp -o test -lboost_system -lboost_filesystem
-isystem tells the compiler where to look for system header files. And -L tells the linker where to look for libraries. It's not obvious if you need the boost filesystem or system libraries based on your code snippet.
If you're unable to read the boost headers or shared libraries on the remote server, this is unrelated to your question. Contact your sys admin for help.

Boost.Python can't find pyconfig.h. Where does it need to go?

I've written a very simple c++ function in main.cpp:
#include <iostream>
using namespace std;
int SomeCalculation(float x){
int decision = 0;
if (x > 1){
decision = 1;
}
return decision;
}
I'm now trying to compile this as a shared library using Boost.Python. For this I created decision.cpp:
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(decision)
{
using namespace boost::python;
def("main", main);
}
Unfortunately I get the following error:
In file included from /usr/include/boost/python/detail/prefix.hpp:13:0,
from /usr/include/boost/python/args.hpp:8,
from /usr/include/boost/python.hpp:11,
from decision.cpp:1:
/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: File or folder does not exist.
compilation terminated.
Since I had no clue of where this file could be I did a simple sudo find / -name pyconfig.h, which found several pyconfig.h files. So I simply copied what to me seemed the most general version of the file, to the folder in which I'm working:
cp /usr/include/python2.7/pyconfig.h /home/kram/c++/cmod/pyconfig.h
Running my compile command again (g++ -fPIC -g -ggdb -c decision.cpp -o decision.so) gives me the same error as before though.
Does anybody know how I can solve this pyconfig.h dependency?
[edit] Added pieces of code
Try command:
g++ -g -shared -fPIC -I/usr/include/python2.7 decision.cpp -lpython2.7 -lboost_python -o decision.so

g++ makefile, include error, no such file or directory

I am trying to include the following headers:
#include <libs/serialization/example/portable_binary_iarchive.hpp>
#include <libs/serialization/example/portable_binary_oarchive.hpp>
These files are located in a path like:
/home/nobody/boost_1_45_0/libs/serialization/example/portable_binary_iarchive.hpp
In my Makefile, I have added:
-I/home/nobody/boost_1_45_0/libs
However, when I compile, I get the error messages like:
error: libs/serialization/example/portable_binary_iarchive.hpp: No such file or directory
Can anybody tell me what I am doing wrong here? I am also including boost libraries like
#include <boost/archive/binary_oarchive.hpp>
However, to get those, it is sufficient to do in my Makefile:
-I/usr/include/boost
Why doesn't this work for the headers in the other location? How should I change my Makefile? The first statement current looks like this:
test: test.o
g++ -O3 -ffast-math -funroll-loops -ansi -pedantic-errors -L/usr/lib -lboost_filesystem -lboost_serialization -lboost_iostreams -lz -I/usr/include/boost -I/home/nobody/boost_1_45_0/libs -o test test.o
To get
#include <libs/serialization/example/portable_binary_iarchive.hpp>
from directory
/home/nobody/boost_1_45_0/libs/serialization/example/portable_binary_iarchive.hpp
your Makefile needs
-I/home/nobody/boost_1_45_0
Notice that I omitted the /libs from the end. That's because your #include directive already lists that directory.
As for your second example, is the file you want at this location:
/usr/include/boost/boost/archive/binary_oarchive.hpp
^^^^^ (repeated boost here)
If not g++ is likely defaulting to /usr/include as the search space for
#include <boost/archive/binary_oarchive.hpp>
Ie., your
-I/usr/include/boost
is useless to the compiler.