Home>

Currently, I am writing a C ++ program using libcmaes on Ubuntu on VirtualBox.
I've reached the point where it works on ubuntu, but for some reason I decided to run it on Windows.
So I tried to install mingw-w64 for cross-compilation, but I get a link error.
Please teach me how to compile without any problems.

I can compile with g ++ without any problem, but when I replace the command with x86_64-w64-mingw32-g ++, I get the following error.

$sudo x86_64-w64-mingw32-g ++ -fopenmp -I/usr/include/eigen3 -I/usr/include/libcmaes -L/usr/lib -std = c ++ 11 -o output.exe source.cpp -lcmaes
/usr/bin/x86_64-w64-mingw32-ld: /tmp/cc67KYiT.o:finish.cpp:(.text+0x146): undefined reference to `libcmaes :: CMAParameters<libcmaes :: GenoPheno<libcmaes :: NoBoundStrategy, libcmaes :: NoScalingStrategy >>:: CMAParameters (std :: vector<double, std :: allocator<double >>const&, double const&, int const&, unsigned long long const&, libcmaes :: GenoPheno<libcmaes :: NoBoundStrategy, libcmaes: : NoScalingStrategy>const&)'
/usr/bin/x86_64-w64-mingw32-ld: /tmp/cc67KYiT.o:finish.cpp:(.text+0x166): undefined reference to `libcmaes :: CMAParameters<libcmaes :: GenoPheno<libcmaes :: NoBoundStrategy, libcmaes :: NoScalingStrategy >>:: ~ CMAParameters ()'
collect2: error: ld returned 1 exit status

The source code is shown below.
Although it is different from the one I am actually working on, the same error occurred even with a simplified sample code of libcmaes, so I will describe it.

#include<iostream>
#include<cmaes.h>
using namespace libcmaes;
int main ()
{
  int dim = 10;// problem dimensions.
  std :: vector<double>x0 (dim, 20.0);
  double sigma = 0.1;
  CMAParameters<>cmaparams (x0, sigma);
  return 0;
}


Postscript:
I couldn't specify include and lib in mingw in the run-time command, so I fixed it.
However, there was no change in the results.

$sudo x86_64-w64-mingw32-g ++ -fopenmp -I/usr/x86_64-w64-mingw32/include/eigen3 -I/usr/x86_64-w64-mingw32/include/libcmaes -L/usr/x86_64-w64-mingw32/lib -static-libstdc ++ -static-libgcc -std = c ++ 11 -o output.exe source.cpp -lcmaes
  • Answer # 1

    First, I prepared a cross-compile environment. I originally included cmake, so I omitted it.

    sudo apt install g ++-mingw-w64-x86-64

    next,
    https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/cross_compiling/Mingw
    According totoolchanin-x86_64-mingw32.cmakeWas created as follows.

    # the name of the target operating system
    SET (CMAKE_SYSTEM_NAME Windows)
    # which compilers to use for C and C ++
    SET (CMAKE_C_COMPILER x86_64-w64-mingw32-gcc)
    SET (CMAKE_CXX_COMPILER x86_64-w64-mingw32-g ++)
    SET (CMAKE_RC_COMPILER x86_64-w64-mingw32-windres)
    # here is the target environment located
    SET (CMAKE_FIND_ROOT_PATH/usr/x86_64-w64-mingw32/home/yumetodo/mingw-install)
    #adjust the default behavior of the FIND_XXX () commands:
    # search headers and libraries in the target environment, search programs in the host environment
    set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

    I introduced libcmaes and its dependent packages as follows:

    git clone https://gitlab.com/libeigen/eigen.git -b 3.3
    cd eigen
    mkdir build
    cd build
    cmake -DCMAKE_TOOLCHAIN_FILE = ~/toolchanin-x86_64-mingw32.cmake -DCMAKE_INSTALL_PREFIX =/home/yumetodo/mingw-install ..
    make install
    cd ~
    git clone https://github.com/google/glog.git -b v0.4.0 --depth = 1
    cd glog
    mkdir build
    cd build
    cmake -DCMAKE_TOOLCHAIN_FILE = ~/toolchanin-x86_64-mingw32.cmake -DCMAKE_INSTALL_PREFIX =/home/yumetodo/mingw-install ..
    cmake --build.
    make install
    cd ~
    git clone https://github.com/gflags/gflags.git -b v2.2.2 --depth = 1
    cd gflags
    mkdir build
    cd build
    cmake -DCMAKE_TOOLCHAIN_FILE = ~/toolchanin-x86_64-mingw32.cmake -DCMAKE_INSTALL_PREFIX =/home/yumetodo/mingw-install ..
    make -j2
    make install
    cd ~
    git clone https://github.com/beniz/libcmaes.git -b 0.9.6.1 --depth = 1
    cd libcmaes
    mkdir build
    cd build
    cmake -DCMAKE_TOOLCHAIN_FILE = ~/toolchanin-x86_64-mingw32.cmake -DCMAKE_INSTALL_PREFIX =/home/yumetodo/mingw-install ..
    make -j4
    make install

    Then the source file presentedsrc.cppCreated at hand as, in the same directorybuild.shI created the following as.

    #!/bin/bash
    export PKG_CONFIG_LIBDIR = ~/mingw-install/share/pkgconfig /: ~/mingw-install/lib/pkgconfig /
    pkg-config --cflags --libs libcmaes eigen3
    x86_64-w64-mingw32-g ++ src.cpp -o foo `pkg-config --cflags --libs libcmaes eigen3`
    $chmod + x build.sh
    $./build.sh
    -fPIC -std = c ++ 11 -fopenmp -I/home/yumetodo/mingw-install/include/eigen3 -I/home/yumetodo/mingw-install/include/libcmaes -L/home/yumetodo/mingw-install/lib -lcmaes

    After all, isn't it that the lib specification is not good?