Debian Linux下用MinGW交叉编译含CPLEX库的Windows可执行文件遇阻
First, let's address your core question: this approach is technically feasible, but it requires fixing mismatched headers and handling C++ ABI differences between Visual Studio (used to build CPLEX Windows libraries) and MinGW. Let's break down your issues and how to resolve them.
1. Root Cause of the generic.h Not Found Error
You're mixing Linux CPLEX headers with Windows CPLEX libraries, which causes platform-specific header mismatches. The generic.h file is a Windows-only internal header included by CPLEX's cross-platform headers (like ilosys.h) when targeting Windows. The Linux version of CPLEX headers omits this file (since it's irrelevant to Linux builds), so when you compile for Windows using Linux headers, the preprocessor can't locate it.
Fix for Missing generic.h
Replace your Linux CPLEX headers with the full set of Windows CPLEX headers:
- From your Windows CPLEX Studio installation, copy the entire
concert/include,cplex/include, andopl/includedirectories to your Debian machine (e.g., to/opt/ibm/ILOG/CPLEX_Studio128_win/include). - Update your compilation script to use these Windows headers instead of the Linux ones:
# INCLUDES (updated to Windows headers) OPL_INCL='-I/opt/ibm/ILOG/CPLEX_Studio128_win/include/opl' CPL_INCL='-I/opt/ibm/ILOG/CPLEX_Studio128_win/include/cplex' CON_INCL='-I/opt/ibm/ILOG/CPLEX_Studio128_win/include/concert' - Add Windows platform defines to your flags to ensure CPLEX headers load Windows-specific code:
FLAGS='-DIL_STD -DILOUSEMT -D_REENTRANT -DWIN32 -D_WIN32'
2. Handling Linker Issues (and VS-MinGW ABI Differences)
When you tested with g++ (native Linux compilation), you got a "cannot find -lcplex" error because you were pointing to Windows libraries instead of Linux ones—this is expected, but it highlights a bigger issue:
CPLEX's Windows static libraries (stat_mda folder) are compiled with Visual Studio, which uses a different C++ ABI than MinGW. Directly linking these static libraries will likely lead to symbol mangling errors or runtime crashes, even if compilation succeeds.
Workaround: Use CPLEX Dynamic Libraries (DLLs)
The most reliable way to avoid ABI conflicts is to link against CPLEX's Windows dynamic libraries:
- Copy the CPLEX Windows DLLs (e.g.,
cplex128.dll,concert.dll,opl.dll) from your Windows installation to Debian (e.g.,/opt/ibm/ILOG/CPLEX_Studio128_win/lib/x64_windows_vs2017/bin). - Generate a MinGW-compatible import library from the DLL:
- Install
gendefanddlltoolon Debian viasudo apt-get install mingw-w64-tools. - For each DLL, run:
gendef /opt/ibm/ILOG/CPLEX_Studio128_win/lib/x64_windows_vs2017/bin/cplex128.dll dlltool -d cplex128.def -l libcplex128.a
- Install
- Update your linker flags to use these generated import libraries, and add CPLEX's dynamic linking macro:
# Add dynamic linking macro to FLAGS FLAGS='-DIL_STD -DILOUSEMT -D_REENTRANT -DWIN32 -D_WIN32 -DILOCPLEXDYNAMIC' # Linker paths (point to where you generated .a files) OPL_LIB='-L/opt/ibm/ILOG/CPLEX_Studio128_win/mingw_libs' CPL_LIB='-L/opt/ibm/ILOG/CPLEX_Studio128_win/mingw_libs' CON_LIB='-L/opt/ibm/ILOG/CPLEX_Studio128_win/mingw_libs' # Link order matters for CPLEX: concert -> cplex -> opl /usr/bin/x86_64-w64-mingw32-g++ $FLAGS $OPL_INCL $CPL_INCL $CON_INCL $EXTRA main.cpp $OPL_LIB $CON_LIB $CPL_LIB -lconcert -lcplex128 -lopl -lm -lpthread - When running the executable on Windows, ensure you place the CPLEX DLLs in the same directory as the
.exeor in a system PATH folder.
3. Final Notes on Feasibility
While this approach works, keep in mind:
- IBM does not officially support cross-compiling CPLEX applications this way, so you may hit edge cases or undocumented issues.
- Always test the compiled executable thoroughly on Windows to catch any runtime ABI-related bugs.
- For simpler projects, consider using a Windows VM with CPLEX installed, but your cross-compilation approach is valid for saving storage space.
内容的提问来源于stack exchange,提问作者Greg




