0

I'm trying to run a helloworld program which uses boost filesystem. I'm on Windows with MinGW 8.1 and boost 1.70. The problem is that, although everything compiles, the program doesn't run. I mean, it runs but doesn't print anything, which means the main function is not even executed:

#include <boost/filesystem.hpp>
#include <iostream>

using namespace std;
using namespace std::string_literals;
namespace fs = boost::filesystem;

int main()
{
    cout << "Hello Boost!" << endl;

    fs::path abHome{"C:/Users/Me"s};
    fs::path jsonFile = abHome / "jsonFile.json"s;
    if (!fs::exists(jsonFile)) {
        cout << "Creating json file from scratch." << endl;
    }
}

"Hello Boost" isn't ever printed to the console. I've compiled with both CMake and g++ from command line to try to better understand what's going on:

g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -lboost_filesystem-mgw81-mt-x64-1_70 -lboost_system-mgw81-mt-x64-1_70 -I"C:/Code/boost_1_70_0"

I've compiled boost for MinGW by following the guide and everything went well, in the output folder I see many different versions of each library based on the default targets (I haven't really picked them, just went with the defaults).

How can I debug the launch of main.exe to see what's causing the crash? It's been many years since I wrote C++ so I need help to get back on track! :)

Etchelon
  • 832
  • 11
  • 27
  • 1
    First, how did you conclude that the issue is with boost? Did you try a simple hello world program that doesn't use boost? – PaulMcKenzie Jun 06 '19 at 18:07
  • That code works for me. Try starting with a simpler "hello world" program, and gradually add Boost.Filesystem parts so you can identify what exactly is causing problems with your setup. – JaMiT Jun 06 '19 at 23:12
  • I concluded that it has something to do with boost because if I comment out the lines of code that use fs the “hello boost” is actually printed out :) even if I leave the import from boost uncommented it works, so it has something to do with the boost code that is in the linked dll, right? – Etchelon Jun 07 '19 at 05:36
  • 2
    Have you added `C:/Code/boost_1_70_0/stage/lib` to your `$PATH` so that `Windows` can find `libboost_filesystem-mgw81-mt-x64-1_70.dll`? – kenba Jun 07 '19 at 07:14
  • @kenba that was it... can you explain to me why I need that? The build executable for this ridiculously short program is 3.7MB in size and the library I linked is static, so I thought it would be included in the .exe! – Etchelon Jun 07 '19 at 08:21
  • The library that you linked must have been shared, otherwise it would not have needed the `dll` file... It is quite normal to build `boost` as `shared` libraries for `MinGw` as some libraries are required to be `shared`, see [boost installation in Windows](https://stackoverflow.com/questions/35217511/boost-1-60-0-zip-installation-in-windows/35223257#35223257) – kenba Jun 07 '19 at 09:50

1 Answers1

0

The problem was, as @kenba pointed out, that the dynamic linking of the boost dlls was failing. I erroneously thought I had linked the static version of the boost libraries. To actually achieve that I should have used this command:

g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -l:"libboost_filesystem-mgw81-mt-x64-1_70.a" -l:"libboost_system-mgw81-mt-x64-1_70.a" -I"C:/Code/boost_1_70_0"

instead of the one I posted in the OP.

Etchelon
  • 832
  • 11
  • 27