Enabling OpenMPI on Linux - c++

I'm trying to enable OpenMPI on Ubuntu using CodeBlocks. I have already downloaded it using:
sudo apt-get install -y  autotools-dev g++ build-essential
openmpi1.6-bin openmpi1.6-doc libopenmpi1.6-dev
Afterwards I tried to run the following code:
#include <stdio.h>
#include <mpi.h>
int main(int argc, char **argv)
{
int size, rank;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
printf("SIZE = %d RANK = %d\n",size,rank);
MPI_Finalize();
return(0);
}
But I got this error:
fatal error: mpi.h: No such file or directory
Then I changed the c++ compiler from g++ to mpicxx as it's recommended here: FAQ: Compiling MPI.
But when I try to run my code now, I get the following bunch of errors:
openmpi.cpp|| undefined reference to `MPI_Init'|
openmpi.cpp|| undefined reference to `ompi_mpi_comm_world'|
openmpi.cpp|| undefined reference to `MPI_Comm_size'|
openmpi.cpp|| undefined reference to `ompi_mpi_comm_world'|
openmpi.cpp|| undefined reference to `MPI_Comm_rank'|
openmpi.cpp|| undefined reference to `MPI_Finalize'|
[...]
I assume, I have to add the Path or link the library, but I'm not sure how to achieve this. I also don't know where OpenMPI has been installed on my machine.
I have downloaded the folder from the website, as well, if that can help.
Update
After adding mpicxx to the linker and -pthread -L/opt/openmpi/lib -lmpi_cxx -lmpi -ldl -lm -Wl,--export-dynamic -lrt -lnsl -lutil -lm -ldl as additional flags, I finally can compile the code.
But now I get another error:
[[INVALID],INVALID] ORTE_ERROR_LOG: A system-required executable either could not be found or was not executable by this user in file ess_singleton_module_c at line 231
[...]
Soory! You were supposed to get help about:
orte_init:startup:internal-failure
But I couldn't open the file:
/usr/share/openmpi/help-orte-runtime: No such file or directory.

Most MPI libraries come with special compiler wrappers, e.g.:
mpicc for the C compiler;
mpic++/mpiCC/mpicxx for the C++ compiler;
mpif77/mpif90/mpif95 for the Fortran compiler;
other distribution-specific wrappers, e.g. mpiicc for Intel MPI library with Intel C Compiler.
These wrappers provide all options that are needed by the compiler in order to find the include files and link the proper libraries.
Therefore you have to update your project settings and change both the compiler and the linker to mpicxx.
Another option would be to run the following command:
mpicxx -showme:link
It will give you a list of options that you should add to the linker flags in your project's settings in order to properly link the executable with Open MPI.

Related

Freeglut 64 bit program linking errors

I'm trying to compile a 64 bit version of an OpenGL C++ program using freeglut. I followed the exact instructions on this website to set up freeglut with MinGW. I have the header files in C:\MinGW\include\GL, I have the 32 bit libraries in C:\MinGW\lib and 64 bit libraries in C:\MinGW\lib\x64, and I have the 64 bit freeglut.dll in my project directory. However, even the simplest of OpenGL programs don't link successfully...
My code is minimal:
// test.cpp
#include <GL/glut.h>
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
}
And I compile it with the exact commands given on the readme/on the website:
g++ -c -o test.o test.cpp -I"C:\MinGW\include"
g++ -o test.exe test.o -L"C:\MinGW\lib\x64" -lfreeglut -lopengl32 -Wl,--subsystem,windows
(except of course I changed the directories and changed gcc to g++)
The compile runs fine, but linking throws these error messages:
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: test.o:test.cpp:(.text+0x1c): undefined reference to `_imp____glutInitWithExit#12'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: test.o:test.cpp:(.text+0x3f): undefined reference to `_imp____glutCreateWindowWithExit#8'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: test.o:test.cpp:(.text+0x61): undefined reference to `_imp____glutCreateMenuWithExit#8'
collect2.exe: error: ld returned 1 exit status
I've tried to change -lopengl32 in the command to -lopengl64 and -lopengl, but the linker couldn't find those libraries.
I can't answer your specific case, but another approach would be to use mingw-w64, with MSYS2 as a package manager. The old mingw.org project is not well maintained, unlike mingw-w64 and the MSYS2 package database. There is probably some mismatch between how your precompiled binaries were built and the compiler you are currently using.
On my system I compiled your code with g++ -o gl gl.cpp -lfreeglut and it worked first time .
How to install MinGW-w64 and MSYS2?
Then use pacman -Ss glut to find the freeglut package.

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.

Fail to use SDL with MinGW on windows

I wanted to start learning to program with SDL. So I download x86 for windows,
put all the lib and the include in MinGW. But when I compile it doesn't know the SDL functions exist.
# define SDL_MAIN_HANDLED // somehow it want it to not define its own main
#include <iostream>
#include <sdl2/SDL.h>
using namespace std;
int main( int argc, char* argv[] ) {
SDL_SetMainReady(); // just for check
return 0;
}
I read that the linking need to be in specific order (mingw32, SDL2main and then libSDL2), But I think Eclipse run a wrong compiling command.
The eclipse command:
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\fire.o" "..\\src\\fire.cpp"
g++ -o fire.exe "src\\fire.o" -lmingw32 -lSDL2main -llibSDL2
src\fire.o: In function `main':
C:\Or\C++\Projects\fire\Debug/../src/fire.cpp:16: undefined reference to `SDL_SetMainReady'
collect2.exe: error: ld returned 1 exit status
Do you think I miss something?
I think you need to change -llibSDL2 to -lSDL2.
Ok I solve it. I'm not sure if the problem was lack of support on 32 bit or the fact that minGW and SDL were from different compilers that probably didn't match..
But what I did is to delete minGW from my pc and download WinBuild. WinBuild its a download manager that offer a lot of libs and tools, include minGW64 bit and SDL.
The advantage is that they were all compile from the same compiler with the same configurations.
after that i change the path to minGW in to the new 64 bit path inside WinBuild folder, add g++ from Winbuild the to the path as well and restart.
Then, adding and linking work without any problem!
I still need to put # define SDL_MAIN_HANDLED on the start to make it work, but its work!

undefined reference to `shm_open' already with -L /lib -lrt -lpthread

I just want to use the boost library to create a shared memory on an ARM system. It work fine if you want to compile it only under ubuntu. However, when I want to cross compile it with TI's CCSv6 and angstrom toolchain, it keep pushing errors.
Because I do not know how to write a makefile for cross compile, I think using TI their own IDE might be a good choice to avoid further problems.
Here is my code and print out of build console.
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
using namespace boost::interprocess;
int main()
{
shared_memory_object shdmem{open_or_create, "Boost1", read_write};
shdmem.truncate(1024);
mapped_region region{shdmem, read_write};
}
g++ -std=c++0x -I/usr/include -O0 -g3 -Wall -c -fmessage-length=0 -L /lib -lrt -lpthread -fPIC
The IDE called Code Composer Studio has cross compile settings as below:
Prefix: arm-angstrom-linux-gnueabi-
Path: /usr/local/oecore-x86_64/sysroots/x86_64-angstromsdk-linux/usr/bin/armv5te-angstrom-linux-gnueabi
Build Console:
/usr/include/boost/interprocess/shared_memory_object.hpp:309: undefined reference to shm_open'
/usr/include/boost/interprocess/shared_memory_object.hpp:315: undefined reference toshm_open'
/usr/include/boost/interprocess/shared_memory_object.hpp:327: undefined reference to shm_open'
/usr/include/boost/interprocess/shared_memory_object.hpp:334: undefined reference toshm_open'
collect2: ld returned 1 exit status
make: *** [test] Error 1
undefined reference to shm_open' means it cannot find -lrt for ARM.
In your build command line you need to specify include and library paths to ARM built libraries, not to Ubuntu ones. So -I/usr/include and -L /lib is wrong.
Also you need boost built for ARM, although if you just want to use interprocess library then boost headers should be enough. But you need to copy them into different location because including them from /usr/include includes also other headers specific to Ubuntu.
You can use the cross compiler IDE you mentioned or arm g++ cross compiler which you can install by:
sudo apt-get install g++-arm-linux-gnueabihf. Some headers and libraries for ARM will be installed too.

Boost.MPI on Ubuntu 12.04

Background
Hi All,
I'm trying to use Boost::MPI, at the moment I'm just trying to run the simple first example from the tutorial. I am having trouble building/running it.
I installed boost using apt-get and installed boost_mpi & boost_serialization (1.48.0) using synaptic package manager. I installed MPICH2 using apt-get.
Even though OpenMPI was never explicitly installed it appears to be on my system, I assume this is a dependency for Boost::MPI but it appears MPICH2 and OpenMPI are treading on each other's toes.
Info
If I build using
g++ test.cpp -I/usr/include/mpich2 -L/usr/lib -lboost_mpi -lboost_serialization
then run using
mpiexec -n 2 ./a.out
It throws a bunch of errors which seem to come from OpenMPI. If I try and build by linking against the OpenMPI library using
g++ test.cpp -L/usr/lib -lboost_mpi -lboost_serialization -lmpi -I/usr/include/openmpi
I get the following errors:
/usr/bin/ld: /tmp/ccJ5ezv7.o: undefined reference to symbol 'ompi_op_set_cxx_callback'
/usr/bin/ld: note: 'ompi_op_set_cxx_callback' is defined in DSO /usr/lib/libmpi.so.0 so try adding it to the linker command line
/usr/lib/libmpi.so.0: could not read symbols: Invalid operation
If I try building using mpic++ with the following command
mpic++ test.cpp -lboost_mpi -lboost_serialization
It will not link returning a bunch of errors of the form
/usr/lib/libmpich.so: undefined reference to `MPL_trid'
so I tried linking against libmpi i.e.
mpic++ test.cpp -lboost_mpi -lboost_serialization -lmpi
This builds but on running with mpiexec yields the following errors
Fatal error in PMPI_Errhandler_set: Invalid communicator, error stack:
PMPI_Errhandler_set(118): MPI_Errhandler_set(comm=0x370500, errh=0x370be0) failed
PMPI_Errhandler_set(70).: Invalid communicator
Question
It seems to me that somehow OpenMPI and MPICH2 are getting intertwined where there really shouldn't. Does anybody know how I can build against only OpenMPI or MPICH2 then run using the correct mpiexec?
Mixing code compiled against different MPI libraries is not supported in general. If your Boost::MPI is linked against Open MPI, then you must use Open MPI for the rest of your application.
To get the mpic++ as well as the other compiler wrappers and all the header files you should install the -dev package for Open MPI. If the library has been installed as a dependency then only the run-time part will be there.