I'm trying to build an executable from just the following code (say it's in the file kt.cu
):
#include <boost/program_options.hpp>
int main(int argc, char** argv)
{
boost::program_options::options_description options("Options");
return 0;
}
Here are 4 possible ways of building it, all should work and result in a binary with no linker errors:
- Compile with CUDA nvcc, link with CUDA nvcc
- Compile with CUDA nvcc, link with g++
- Compile with g++, link with CUDA nvcc
- Compile with g++, link with g++
The relevant compilation and linking commands:
Compile with CUDA nvcc:
nvcc -std=c++11 -c kt.cu
Compile with g++ (forcing it to be compiled as a .cpp file would):
g++ -x c++ -std=c++11 -c kt.cu
Link with CUDA nvcc:
nvcc -o kt -lboost_program_options -lcudart -L/usr/local/cuda/lib64 kt.o
Link with g++:
g++ -o kt -rdynamic -lboost_program_options -lcudart -L/usr/local/cuda/lib64 kt.o
If I compile with g++, linking works regardless of which linker I chose (i.e. options 3 and 4 work). If I compile with nvcc, linking fails, regardless of the linker I chose (i.e. options 1 and 2 fail)
Here's the error message I get:
tmpxft_00003de6_00000000-4_kt.cudafe1.cpp:(.text+0x76): undefined reference to `boost::program_options::options_description::options_description(std::string const&, unsigned int, unsigned int)'
collect2: error: ld returned 1 exit status
Why is this failing? And what should I do to fix/work around this?
Notes:
- I'm using Debian stretch with regular
apt-get dist-upgrade
s. - I don't think there's something special about boost::program_options, i.e. it happens with other boost libs when I also use them. It's probably not boost-specific either, although I can't really say.
- Versions (which shouldn't matter I think): CUDA 7.5, Boost 1.58, g++4.9.3. Also tried g++ 4.8.5 with same effect. Also tried compiling and linmking with g++ 5.2.1 only, that worked as well.
- Edit: I can confirm this does not occur on Fedora 20, with g++ 4.8.3, Boost 1.54, CUDA 7.5. Can it be something with the Boost version?