问题
The cmake file below is the source of problem because I can compile the code with mpic++ directly and without using cmake.
Why the cmake file below doesn't work?
Current cmake file:
cmake_minimum_required(VERSION 2.8)
project(boost_mpi_cmake)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_executable(test test.cpp)
find_package(Boost REQUIRED mpi system)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(test ${Boost_LIBRARIES})
find_package(MPI REQUIRED)
include_directories(${MPI_CXX_INCLUDE_PATH})
target_link_libraries(test ${MPI_CXX_LIBRARIES})
test.cpp:
#include <boost/mpi.hpp>
#include <iostream>
#include <string>
namespace mpi = boost::mpi;
int main()
{
mpi::environment env;
mpi::communicator world;
std::string s(env.processor_name());
std::cout << s << "\n";
return 0;
}
Error:
Undefined symbols for architecture x86_64:
"boost::mpi::environment::processor_name[abi:cxx11]()", referenced from:
_main in test.cpp.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make[2]: *** [test] Error 1
make[1]: *** [CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2
Compile without cmake works:
mpic++ test.cpp -lboost_mpi
来源:https://stackoverflow.com/questions/41768921/write-cmakelists-txt-for-boostmpi