1

I have the program Ex_04.cpp which has this code:

#include <iterator>
#include <iostream>
#include <vector>
#include <boost/filesystem/fstream.hpp>

#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/filesystem.hpp>
#undef BOOST_NO_CXX11_SCOPED_ENUMS


namespace fs = boost::filesystem;

int write_to_file()
{
    const auto dir = "Test.txt";
    fs::path pp{dir};
    try{
        if( create_directory(pp) ){
            rename(pp, dir);
        }
    }catch(fs::filesystem_error &e){
        std::cerr << e.what() << std::endl;
    }
    return 0;

}

int main()
{
    std::cout << "Hello world!" << std::endl;
    return 0;
}

My settings.json file looks like this:

{
    "code-runner.executorMap": {
        "cpp": "cd $dir && g++  -L'C:\\boost_1_81_0\\boost_installed\\lib' -I'C:\\boost_1_81_0\\boost_installed\\include\\boost-1_81' -Werror -Wall -Wextra -Wpedantic -o $fileNameWithoutExt *.cpp  && ./$fileNameWithoutExt.exe"
    },
    "editor.tokenColorCustomizations": {
        "textMateRules": [
            {
                "scope": "googletest.failed",
                "settings": {
                    "foreground": "#f00"
                }
            },
            {
                "scope": "googletest.passed",
                "settings": {
                    "foreground": "#0f0"
                }
            },
            {
                "scope": "googletest.run",
                "settings": {
                    "foreground": "#0f0"
                }
            }
        ]
    }
}

I have not changed the files tasks.json, launch.json and c_cpp_properties.json.
The error I get when I try to compile is:

C:/msys64/mingw64/x86_64-w64-mingw32/bin/ld.exe: C:\Users\tvmendes\AppData\Local\Temp\cc1GfM0X.o:Ex_04.cpp:
    (.text$_ZN5boost10filesystem16create_directoryERKNS0_4pathE[_ZN5boost10filesystem16create_directoryERKNS0_4pathE]+0x1f): undefined reference to boost::filesystem::detail::create_directory(boost::filesystem::path const&, boost::filesystem::path const*, boost::system::error_code*)
C:/msys64/mingw64/x86_64-w64-mingw32/bin/ld.exe: C:\Users\tvmendes\AppData\Local\Temp\cc1GfM0X.o:Ex_04.cpp:
    (.text$ZN5boost10filesystem6renameERKNS0_4pathES3[ZN5boost10filesystem6renameERKNS0_4pathES3]+0x22): undefined reference to boost::filesystem::detail::rename(boost::filesystem::path const&, boost::filesystem::path const&, boost::system::error_code*)
C:/msys64/mingw64/x86_64-w64-mingw32/bin/ld.exe: C:\Users\tvmendes\AppData\Local\Temp\cc1GfM0X.o:Ex_04.cpp:
    (.text$_ZNK5boost10filesystem4path9assign_opclIcEEvPKT_S6_PKSt7codecvtIwciE[_ZNK5boost10filesystem4path9assign_opclIcEEvPKT_S6_PKSt7codecvtIwciE]+0x44): undefined reference to boost::filesystem::detail::path_traits::convert(char const*, char const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&, std::codecvt<wchar_t, char, int> const*)
collect2.exe: error: ld returned 1 exit status

I tried to change the settings.json file by adding the flag -l like this:

"cd $dir && g++ -L'C:\\boost_1_81_0\\boost_installed\\lib' -I'C:\\boost_1_81_0\\boost_installed\\include\\boost-1_81' -l'C:\\boost_1_81_0\\boost_installed\\lib' -Werror -Wall -Wextra -Wpedantic -o $fileNameWithoutExt *.cpp && ./$fileNameWithoutExt.exe"

to link the library filesystem but I always get the error:

C:/msys64/mingw64/x86_64-w64-mingw32/bin/ld.exe: cannot find -lC:\boost_1_81_0\boost_installed\lib: Invalid argument
collect2.exe: error: ld returned 1 exit status

I have checked and the filesystem library exists at that directory.

My operation system is Microsoft Windows 10 Enterprise.

greybeard
  • 2,249
  • 8
  • 30
  • 66
  • You're compiling and linking all .cpp files (`*.cpp`) together. – molbdnilo Mar 31 '23 at 08:10
  • 1
    I would worry about the multiply defined `main` before you worry about the boost library. – john Mar 31 '23 at 08:11
  • 2
    Your other error (in settings.json) is because you should be saying `-L` for the library directory not `-l`. `-l` is for the names of individual libraries. – john Mar 31 '23 at 08:13
  • In fact looking at your settings you seem to be specifying the library directory twice, once with `-L` and once with `-l`. You should not to do that, for a library directory only `-L` is needed. – john Mar 31 '23 at 08:14
  • @john The error was incorrect. I have just updated it and the multiply defined main issue is now gone. – Tiago Mendes Mar 31 '23 at 08:26
  • @john My understanding is that -L is for library directory and -l is for defining which libraries will be used for linking. In the settings.json i only use `-L` and `-I` flags. – Tiago Mendes Mar 31 '23 at 08:28
  • @molbdnilo How can i change the compilation line then ? – Tiago Mendes Mar 31 '23 at 08:30
  • @TiagoMendes Your understanding is correct, but your settings.json above clearly has `-l'C:\\boost_1_81_0\\boost_installed\\lib'` which is incorrect. (Which is what the error message is telling you). – john Mar 31 '23 at 08:30
  • @TiagoMendes There's some confusion here with upper and lower case, but `-I` is for directories to search for header files, `-L` is for directories to search for libraries and `-l` is for the names of the libraries to link. It's this last part that you seem to be missing or have got incorrect. – john Mar 31 '23 at 08:35
  • @TiagoMendes If I knew I tell you what the setting for `-l` should be but I've never used boost with MinGW, so I'm not sure. – john Mar 31 '23 at 08:36

1 Answers1

0

There are several issues.

Firstly, don't *.cpp unless your files are intended to be linked together (they can't if multiple contain main).

Second, you were indicating library path (-L), but failed to link to a specific library from that path. Add -lboost_filesystem.

When you do, make sure it comes last on the command line.

Finally, you must make sure that the libraries are found at runtime too. The easiest way is to us LD_LIBRARY_PATH but you can consider rpath linker options too.

 "code-runner.executorMap": {
    "cpp": "cd $dir && g++  -L'/home/sehe/custom/superboost/stage/lib' -I'/home/sehe/custom/superboost' -Werror -Wall -Wextra -Wpedantic $fileName -o $fileNameWithoutExt.exe -lboost_filesystem -lboost_system -lboost_thread  && LD_LIBRARY_PATH='/home/sehe/custom/superboost/stage/lib' ./$fileNameWithoutExt.exe"
}

See it live:

enter image description here

Side Note

As you can see I [re]moved the

 #define BOOST_NO_CXX11_SCOPED_ENUMS
 #undef BOOST_NO_CXX11_SCOPED_ENUMS

If you use defines like this:

  • define them everywhere (the #undef is very suspect)
  • define them before the inclusion of any Boost header!

Failing to do so will violate ODR.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Hi @sehe, I followed your instructions and i got this errors: `C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lboost_filesystem: No such file or directory C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lboost_system: No such file or directory C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lboost_thread: No such file or directory` – Tiago Mendes Mar 31 '23 at 13:46
  • My lib directory is `C:\boost_1_81_0\boost_installed\lib` and has only files like this `libboost_filesystem-vc143-mt-gd-x32-1_81.lib` and a cmake folder. – Tiago Mendes Mar 31 '23 at 13:46
  • Okay. You didn't exactly specify which libraries (_"Using Boost Library in Visual Studio Code with Code Runner"_) so I gave a subset of common libraries you will typically use. If you only have `filesystem`, drop the other two, of course (or build the rest as well) – sehe Mar 31 '23 at 14:31
  • Sorry @sehe, what i meant is that my lib folder has files of the type ´*.lib´. – Tiago Mendes Mar 31 '23 at 15:37
  • Depending on whether those are static libraries or only import libraries you can link them (e.g. using the filename like `libboost_filesystem.lib` instead of `boost_filesystem`). There may also be a CMake way to use statically linked libraries, but I'd try this to see whether your build is even complete - it looks like it may not be. – sehe Mar 31 '23 at 16:03