asutherland asutherland - 11 months ago 40
C++ Question

Linking windows C++ project to boost on command line architecture type issue

I have an external c++ code base I am trying to build that requires BOOST. I have VS2015 community edition and am trying to build from the command line.

I downloaded and built boost using this command:

.\b2 toolset=msvc-14.0 --build-type=complete --abbreviate-paths architecture=64 address-model=64 install -j4

I then am trying to build the project like this:

cl /EHsc -I "C:\Users\alex\Documents\boost_1_60_0" myproj_helper.cpp main.cpp /link /LIBPATH:"C:\Boost\lib" /out:program.exe

When I do this I get the following error.

libboost_thread-vc140-mt-s-1_60.lib(thread.obj) : fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'X86'

I am not sure if this means I built boost incorrectly and need to rebuild with different settings or if I am missing an argument that I need to give cl regarding the architecture or what. I am on a windows 10 home 64 bit machine. How do I fix this?

Answer Source

You are building Boost using the architecture x64, which is good and what you want. The problem is that you're building your project with the default architecture of x86 which conflicts with Boost when linking. Using this documentation, you can setup your environment in the command line to use the x64 architecture.

  1. Set the desired MSVC version's VC directory as the working directory.

    cd "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC"

  2. Run this magic batch script (there's also amd64_x86 which is default, and amd64_arm)

    vcvarsall amd64

If you intended to build for x86, you will just need to configure Boost to build to x86. Like I said, this is the default for MSVC which will make it compatible.