【问题标题】:How to cross compile from Linux to 32-bit Windows executable如何从 Linux 交叉编译为 32 位 Windows 可执行文件
【发布时间】:2018-05-22 15:31:18
【问题描述】:

对于上下文,我正在尝试使用 Linux 机器将源代码编译为适用于 Windows 的 32 位可执行文件。我正在通过apt-get 使用当前的mingw-w64。这是我正在尝试编译的项目ftp://ftp.thegpm.org/projects/tandem/source。更具体地说,17-02-01 zip 文件包含我感兴趣的源代码。 我的第一次尝试是使用tandem-linux 下的Makefile_ubuntu 进行编辑,然后将gcc 替换为mingw 提供的gcc,并修复通过将#includes 添加到.cpp 文件中出现的标头引用问题错误。超级哈克。谁能告诉我一条更光明的道路?

这是我正在使用的生成文件:

#makefile for c++ programs
#change the name of the executable to use for "any" project

EXECUTABLE = ../bin/tandem.exe
#EXECUTABLE = ../bin/p3.exe
LINKCC = $(CXX)

#CXXFLAGS denotes flags for the C++ compiler

CXX = /usr/bin/i686-w64-mingw32-g++-win32

#uncomment this line if you are using gcc 4.x
CXXFLAGS = -m32 -std=gnu++11
#CXXFLAGS = -w -O2 -DGCC4_3
#CXXFLAGS = -w -O2 -DGCC4_3 -DX_P3

#ubuntu 64 bit version
#LDFLAGS = -L/usr/lib/x86_64-linux-gnu/libexpat.a
LDFLAGS = -lpthread -lm -L/usr/lib/x86_64-linux-gnu/libexpat.a
#LDFLAGS = -lpthread -L/usr/lib -lm -lexpat

SRCS := $(wildcard *.cpp)
OBJS := $(patsubst %.cpp,%.o,$(wildcard *.cpp))
DEPS := $(patsubst %.o,%.d,$(OBJS))


all: $(EXECUTABLE)

#define the components of the program, and how to link them
#these components are defined as dependencies; that is they must be up-to-date before the code is linked

$(EXECUTABLE): $(DEPS) $(OBJS)
        $(LINKCC) $(CXXFLAGS) -o $(EXECUTABLE) $(OBJS) $(LDFLAGS)

#specify the dep files depend on the cpp files

%.d: %.cpp
        $(CXX) -M $(CXXFLAGS) $< > $@
        $(CXX) -M $(CXXFLAGS) $< | sed s/\\.o/.d/ > $@



clean:
        -rm $(OBJS) $(EXECUTABLE) $(DEPS) *~

explain:
        @echo "The following info represents the program:"
        @echo "Final exec name: $(EXECUTABLE)"
        @echo "Source files:       $(SRCS)"
        @echo "Object files:       $(OBJS)"
        @echo "Dep files:          $(DEPS)"

depend: $(DEPS)
        @echo "Deps are now up-to-date."

-include $(DEPS)

这是错误:

sudo make -f Makefile_ubuntu
    /usr/bin/i686-w64-mingw32-g++-win32  -m32 -std=gnu++11  -o ../bin/tandem.exe tandem.o p3mprocess.o saxmzdatahandler.o mspectrumcondition.o masscalc.o mprocess.o mreport.o mscore_tandem.o loadmspectrum.o mplugin.o msequenceserver.o saxtaxhandler.o msequencecollection.o mscore.o mrefine.o xmltaxonomy.o mbiomlreport.o saxtandeminputhandler.o saxhandler.o msequtilities.o base64.o saxmodhandler.o mtermmods.o xmlparameter.o saxsaphandler.o saxmzxmlhandler.o saxmzmlhandler.o mxxcleavage.o p3msequenceserver.o mzid_report.o saxbiomlhandler.o p3.o mpmods.o saxgamlhandler.o stdafx.o MSNumpress.o mpam.o -lpthread -lm -L/usr/lib/x86_64-linux-gnu/libexpat.a 
    saxhandler.o:saxhandler.cpp:(.text+0x100): undefined reference to `XML_ParserCreate'
    saxhandler.o:saxhandler.cpp:(.text+0x11d): undefined reference to `XML_SetUserData'
    saxhandler.o:saxhandler.cpp:(.text+0x13b): undefined reference to `XML_SetElementHandler'
    saxhandler.o:saxhandler.cpp:(.text+0x151): undefined reference to `XML_SetCharacterDataHandler'
    saxhandler.o:saxhandler.cpp:(.text+0x1cf): undefined reference to `XML_ParserFree'
    saxhandler.o:saxhandler.cpp:(.text+0x344): undefined reference to `XML_Parse'
    saxhandler.o:saxhandler.cpp:(.text+0x37f): undefined reference to `XML_Parse'
    saxhandler.o:saxhandler.cpp:(.text+0x3bd): undefined reference to `XML_GetErrorCode'
    saxhandler.o:saxhandler.cpp:(.text+0x3d4): undefined reference to `XML_GetCurrentLineNumber'
    collect2: error: ld returned 1 exit status
    Makefile_ubuntu:33: recipe for target '../bin/tandem.exe' failed
    make: *** [../bin/tandem.exe] Error 1

【问题讨论】:

  • 如果您要交叉编译到 Windows,那么您需要链接 Windows 版本的库,而不是 /usr/lib/x86_64-linux-gnu/libexpat.a

标签: c++ linux windows


【解决方案1】:

您(错误地)与/usr/lib/x86_64-linux-gnu/libexpat.a 链接,这是一个 Linux 库(ELF 格式)。您需要获取它的一些 Windows 版本。

顺便说一句,-L/usr/lib/x86_64-linux-gnu/libexpat.a 不正确。因为-L 应该给出一个目录而不是一个要链接的库

最后,最新版本的 Windows 可能有 WSL,这可能对您有用(您将编译一个 Linux,主要是静态链接的可执行文件,它可能在 Windows 的命令行上运行)。

【讨论】:

  • 谢谢。我在 Windows 源代码下看到了 libexpat.lib。这可能是我需要的。我试试看。
猜你喜欢
  • 2011-08-06
  • 2011-02-11
  • 1970-01-01
  • 2014-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-20
  • 2022-06-12
相关资源
最近更新 更多