I am trying to develop a simple program in C++ with the boost library. I develop with Visual Studio 2017 and a remote bash shell of ubuntu to compile and debug.
I installed gdb, gdbserver, all the compiler and the boost library on ubuntu.
Simple programs without boost compile and run without problem from the shell directly as from Visual Studio !
When I compile the following program directly from the ubuntu bash with the following command : g++ test.cpp -std=c++11 -lboost_program_options -o t
It compiles and runs also!
#include <boost/program_options.hpp>
#include <iostream>
using namespace boost::program_options;
int main(int argc, const char *argv[])
{
try
{
options_description desc{ "Options" };
desc.add_options()
("help,h", "Help screen");
variables_map vm;
store(parse_command_line(argc, argv, desc), vm);
notify(vm);
if (vm.count("help"))
std::cout << desc << '\n';
}
catch (const error &ex)
{
std::cerr << ex.what() << '\n';
}
}
But if I put the same code in Visual Studio in a file and try to compile remotely it doesn't work :
1>------ Build started: Project: ACO-PPS, Configuration: Debug x64 ------
1>Validating architecture
1>Validating sources
1>Copying sources remotely to 'localhost'
1>Starting remote build
1>Compiling sources:
1>main.cpp
1>Linking objects
1>/home/marius/projects/ACO-PPS/obj/x64/Debug/main.o : In the function main :
1>/home/marius/projects/ACO-PPS/main.cpp:11 : undefined reference to « boost::program_options::options_description::m_default_line_length »
and so on ...
In the project properties I have included -lboost_program_options
under :
Configuration Properties > C/C++ > All Options > Additional Options
and under :
Configuration Properties > Linker > All Options > Additional Options
What am I doing wrong ?
Below are the rules used by VCLinux for specifying libraries to GCC - from Ion Todirel (MSFT) in an answer on the VCLinux GitHub site.
You will see that
...Additional Options
puts the library before the object files and hence the linker won't look in the library for dependencies. I'd recommend usingLinker - Input - Library Dependencies
and specify the library name,boost_program_options
without the-l
.Linker - General - Additional Library Directories - this adds paths with -L to the linker command line near the beginning of the command line.
Linker - Input - Library Dependencies - this adds the file names with -l at the very end of the linker command line
Linker - Input - Additional Dependencies - this adds the entries verbatim after the object files, and before the Linker - Input - Library Dependencies
Linker - Command Line - Additional Options - this adds the entries verbatim before the object files in the linker command line
Note that the library name given in
Linker - Input - Library Dependencies
is passed togcc
as a-l
command line option; i.e. it should not have thelib
prefix or an extension. For example,libcairo.so
should appear inLinker - Input - Library Dependencies
ascairo
. On the Linux remote,gcc
will search along the path(s) specified inLinker - General - Additional Library Directories
and the default system library search paths looking first forlibcairo.so
(dynamically linked, or shared, library) thenlibcairo.a
(statically linked library).If you have both shared and static libraries on your system, the shared library will be used preferrentially. If you want force linking of the static library see Telling gcc directly to link a library statically.