Boost.MPI gives Undefined symbols for architecture x86_64 - c++

I am trying to run the basic "Hello, World!" example:
#include <boost/mpi/environment.hpp>
#include <boost/mpi/communicator.hpp>
#include <iostream>
namespace mpi = boost::mpi;
int main()
{
mpi::environment env;
mpi::communicator world;
std::cout << "I am process " << world.rank() << " of " << world.size()
<< "." << std::endl;
return 0;
}
I have tried numerous variants for running this program:
mpic++ -I /usr/local/include/ test.cpp -o test -lboost_system
also:
mpic++ -I /usr/local/include/boost test.cpp -o test -lboost_system
and using mpicc, and clang++ as a substitute. Each combination gives the follow error:
Undefined symbols for architecture x86_64:
"boost::mpi::environment::environment(bool)", referenced from:
_main in test-b0215f.o
"boost::mpi::environment::~environment()", referenced from:
_main in test-b0215f.o
"boost::mpi::communicator::communicator()", referenced from:
_main in test-b0215f.o
"boost::mpi::communicator::rank() const", referenced from:
_main in test-b0215f.o
"boost::mpi::communicator::size() const", referenced from:
_main in test-b0215f.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Homebrew says that both MPICH2 and boost1.63.0 are installed. I can confirm that mpic++ runs by compiling and then running this program:
// required MPI include file
#include "mpi.h"
#include <stdio.h>
int main(int argc, char *argv[]) {
int numtasks, rank, len, rc;
char hostname[MPI_MAX_PROCESSOR_NAME];
// initialize MPI
MPI_Init(&argc,&argv);
// get number of tasks
MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
// get my rank
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
// this one is obvious
MPI_Get_processor_name(hostname, &len);
printf ("Number of tasks= %d My rank= %d Running on %s\n", numtasks,rank,hostname);
// do some work with message passing
// done with MPI
MPI_Finalize();
}
Which produces the correct output.
I have also verified that (at least part of) Boost is installed by compiling and successfully running the Boost "Hello, World!"
//
// timer.cpp
// ~~~~~~~~~
//
// Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
t.wait();
std::cout << "Hello, world!" << std::endl;
return 0;
}
with:
clang++ -I /usr/local/include/ timer.cpp -o timer -lboost_system
How can I get the Boost.MPI example to run?

When you get linker errors from boost, you usually forgot to link against a boost library.
There is also a boost_mpi library, so you should compile with
clang++ -I /usr/local/include/ timer.cpp -o timer -lboost_system -lboost_mpi
Note that you need a boost version with support for mpi.
Generally consider http://www.boost.org/doc/libs/1_58_0/doc/html/mpi/getting_started.html
Specifically with homebrew you can check How to build boost with mpi support on homebrew?

Related

Basic practice of dynamic library C++

I am trying dynamic-linking test.
mylib.cpp
#include<iostream>
#include<stdio.h>
using namespace std;
int hello(){
cout<<"Hello,World!"<<endl;
return 1;
}
then compile
g++ -shared -o libmylib.so mylib.cpp
Now there appears the libmylib.so
test.cpp
#include <iostream>
int hello();
int main() {
hello();
std::cout << "Test Finish!\n";
return 0;
}
Try to compile with this,
g++ -o test test.cpp -L ./
There comes the error
Undefined symbols for architecture x86_64:
"hello()", referenced from:
_main in test-37bd2a.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Do I need to add some options or are there something wrong in my source code??
Thank you for your help.
Do I need to add some options
yes, link with the library.
g++ ... -lmylib

link error when accessing std::string members on Mac Yosemite with libc++

I'm trying to compile this kind of code and I get link errors when trying to build using latest Xcode (6.0) on MacOSX Yosemite:
#include <iostream>
#include <string>
int main()
{
auto x = &std::string::size;
std::string hello("hello");
std::cout << (hello.*x)() << std::endl;
return 0;
}
$ g++ --std=c++11 -stdlib=libc++ hello.cpp -o hello
KO:
Undefined symbols for architecture x86_64:
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::size() const", referenced from:
_main in hello-a9a7cd.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Note that the code compiles file using libstdc++:
$ g++ --std=c++11 -stdlib=libstdc++ hello.cpp -o hello
OK
I was having the same problem. See Taking pointer to member std::string::size fails to link with libc++ but works with libstdc++
Basically the solution I came up with is to force the instantiation of the std::string template class.
Here is the proposed solution:
#include <iostream>
#include <string>
template class std::basic_string<char>;
int main()
{
auto x = &std::string::size;
std::string hello("hello");
std::cout << (hello.*x)() << std::endl;
return 0;
}

Compiling boost on Mac OS X Mavericks [duplicate]

This question already has answers here:
C++ Boost: undefined reference to boost::system::generic_category()
(13 answers)
Closed 8 years ago.
There are many threads on this topic, but I've tried some of the solutions and nothing seems to work.
If I install boost with:
brew install booost --c++11
and try to compile the following program tut1.cpp:
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cout << "Usage: tut1 path\n";
return 1;
}
std::cout << argv[1] << " " << file_size(argv[1]) << '\n';
return 0;
}
using
clang -I /usr/local/Cellar/boost/1.55.0_1/include/ -L /usr/local/Cellar/boost/1.55.0_1/lib/ tut1.cpp
and it fails horribly:
Undefined symbols for architecture x86_64:
"boost::filesystem::path::codecvt()", referenced from:
boost::filesystem::path::path<char*>(char* const&, boost::enable_if<boost::filesystem::path_traits::is_pathable<boost::decay<char*>::type>, void>::type*) in tut1-6a2087.o
...
...
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The linking seems to be the problem because if I invoke the -c option the object file gets created.
Any ideas what the hell is going on?
You want to link against boost filesystem and boost system:
-lboost_filesystem -lboost_system
So the complete compile command would be:
clang++ -I /usr/local/opt/boost/include -L /usr/local/opt/boost/lib tut1.cpp -lboost_filesystem -lboost_system

C++ class method not found when compiled

I created a simple class 'Hello' in C++ using header(.h) and definition(.cpp) files. This is the header file content:
#ifndef HELLO_H
#define HELLO_H
#include <string>
namespace test
{
class Hello
{
private:
std::string name;
public:
Hello();
void say_hello();
};
}
#endif
And the definition file content is just as you expected:
#include "Hello.h"
#include <iostream.h>
using namespace test;
Hello::Hello()
{
this->name = "Yoppy Yunhasnawa";
}
void Hello::say_hello()
{
string message = "Hello, " + this->name + ".. Have nice day!";
cout << message << "\n";
}
I included this class to a main.cpp file and use it like this:
#include "Hello.h"
using namespace test;
int main(int argc, char *argv[])
{
Hello* hello = new Hello;
hello->say_hello();
}
When I compiled the main.cpp file with g++ like this,
g++ main.cpp
I got following annoying error:
Undefined symbols for architecture x86_64:
"test::Hello::say_hello()", referenced from:
_main in ccsaoOZa.o
"test::Hello::Hello()", referenced from:
_main in ccsaoOZa.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
However, that error does not appear when I don't call both constructor and say_hello method:
int main(int argc, char *argv[])
{
Hello* hello;// = new Hello;
//hello->say_hello();
}
I use macport GCC 4.7 and I am very sure that my method is there but why this symbol(s) not found error keep appearing? Please show me my mistake. Thank you.
When you invoke g++ main.cpp, compiler performs both compiling AND linking. But the code cannot be linked without Hello.cpp file. So, you have two options: either compile and link separately:
g++ -c main.cpp
g++ -c hello.cpp
gcc main.o hello.o
or compile and link everything at the same time:
g++ main.cpp hello.cpp

#include <lib.h> gives symbol not found, why?

I have this code:
#include <iostream>
#include <mp4.h>
int main (int argc, char * const argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
MP4Read("filename", MP4_DETAILS_ALL );
return 0;
}
And i've added -I/opt/local/include and -L/opt/local/lib to the path (where the mp4 library resides after installing it through macports), but all i get is:
Undefined symbols: "_MP4Read",
referenced from:
_main in main.o ld: symbol(s) not found
Even though XCode finds it and autocompletes properly...
You need to link the library most likely, i.e. add -lmp4 or similar to your linking commands.
You have only specified the paths. You need to link in the mp4 library. Something like the following:
g++ -I /.../ -L /.../ -lmp4 -o out main.cpp
The -L flags tell the compiler where to look, the -l flag tells it what to look for.