使用SDL2时,为什么还会出现“未定义的引用”链接器错误?

问题描述 投票:0回答:1

我已经查看了与此错误直接相关的堆栈溢出的每个其他页面。

我正在尽我所能,但我仍然得到这个错误

未定义的对“SDL_Init”的引用

这是构建日志:

C:\WINDOWS\system32\cmd.exe /C C:/TDM-GCC-64/bin/mingw32-make.exe -j4 SHELL=cmd.exe -e -f  Makefile
"----------Building project:[ testproject - Debug ]----------"
mingw32-make.exe[1]: Entering directory 'C:/Users/Andre/Documents/CodeLiteWorkspace/testproject'
C:/TDM-GCC-64/bin/g++.exe -o ./Debug/testproject @"testproject.txt" -L.
./Debug/main.cpp.o:main.cpp:(.text+0x43): undefined reference to `SDL_Init'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[1]: *** [Debug/testproject] Error 1
testproject.mk:82: recipe for target 'Debug/testproject' failed
mingw32-make.exe[1]: Leaving directory 'C:/Users/Andre/Documents/CodeLiteWorkspace/testproject'
mingw32-make.exe: *** [All] Error 2
Makefile:4: recipe for target 'All' failed
====2 errors, 0 warnings====

这些是我正在使用的构建选项

-std=c++11;-IC:\SDL2\include -LC:\SDL2\lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2

我知道所有内容(编译器和库文件)都是64位二进制文​​件。我发现它给了我同样的错误,但它没有SDL_Init它说WinMain如果我使用正常的int main()入口函数,所以现在我使用int WinMain(),删除了该错误。所以现在我的代码只是由

extern "C"{
    #include <SDL2/SDL.h>
}

int WinMain(){
    SDL_Init( SDL_INIT_EVERYTHING );
    return 0;
}

如果我注释掉SDL_Init函数,程序没有构建错误,但没有做任何事情。

有谁知道这是否与我使用稍微不同的编译器这一事实有关?另外,我不确定,但是从构建日志看起来它实际上并没有使用我提供的构建选项(我正在使用IDE,所以程序管理它)。最后,如果有人知道为什么它要我使用WinMain而不是常规主要功能,并告诉我要修改什么来修复它,那将是值得赞赏的。

生成文件:

##
## Auto Generated makefile by CodeLite IDE
## any manual changes will be erased      
##
## Debug
ProjectName            :=testproject
ConfigurationName      :=Debug
WorkspacePath          :=C:/Users/Andre/Documents/CodeLiteWorkspace
ProjectPath            :=C:/Users/Andre/Documents/CodeLiteWorkspace/testproject
IntermediateDirectory  :=./Debug
OutDir                 := $(IntermediateDirectory)
CurrentFileName        :=
CurrentFilePath        :=
CurrentFileFullPath    :=
User                   :=Andrew900460
Date                   :=20/09/2018
CodeLitePath           :="C:/Program Files/CodeLite"
LinkerName             :=C:/TDM-GCC-64/bin/g++.exe
SharedObjectLinkerName :=C:/TDM-GCC-64/bin/g++.exe -shared -fPIC
ObjectSuffix           :=.o
DependSuffix           :=.o.d
PreprocessSuffix       :=.i
DebugSwitch            :=-g 
IncludeSwitch          :=-I
LibrarySwitch          :=-l
OutputSwitch           :=-o 
LibraryPathSwitch      :=-L
PreprocessorSwitch     :=-D
SourceSwitch           :=-c 
OutputFile             :=$(IntermediateDirectory)/$(ProjectName)
Preprocessors          :=
ObjectSwitch           :=-o 
ArchiveOutputSwitch    := 
PreprocessOnlySwitch   :=-E
ObjectsFileList        :="testproject.txt"
PCHCompileFlags        :=
MakeDirCommand         :=makedir
RcCmpOptions           := 
RcCompilerName         :=C:/TDM-GCC-64/bin/windres.exe
LinkOptions            :=  
IncludePath            :=  $(IncludeSwitch). 
IncludePCH             := 
RcIncludePath          := 
Libs                   := 
ArLibs                 :=  
LibPath                := $(LibraryPathSwitch). 

##
## Common variables
## AR, CXX, CC, AS, CXXFLAGS and CFLAGS can be overriden using an environment variables
##
AR       := C:/TDM-GCC-64/bin/ar.exe rcu
CXX      := C:/TDM-GCC-64/bin/g++.exe
CC       := C:/TDM-GCC-64/bin/gcc.exe
CXXFLAGS := -std=c++11 -IC:\SDL2\include -Dmain=SDL_main -LC:\SDL2\lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2 -mwindows  $(Preprocessors)
CFLAGS   := -g -O0 -Wall  $(Preprocessors)
ASFLAGS  := 
AS       := C:/TDM-GCC-64/bin/as.exe


##
## User defined environment variables
##
CodeLiteDir:=C:\Program Files\CodeLite
Objects0=$(IntermediateDirectory)/main.cpp$(ObjectSuffix) 



Objects=$(Objects0) 

##
## Main Build Targets 
##
.PHONY: all clean PreBuild PrePreBuild PostBuild MakeIntermediateDirs
all: $(OutputFile)

$(OutputFile): $(IntermediateDirectory)/.d $(Objects) 
    @$(MakeDirCommand) $(@D)
    @echo "" > $(IntermediateDirectory)/.d
    @echo $(Objects0)  > $(ObjectsFileList)
    $(LinkerName) $(OutputSwitch)$(OutputFile) @$(ObjectsFileList) $(LibPath) $(Libs) $(LinkOptions)

MakeIntermediateDirs:
    @$(MakeDirCommand) "./Debug"


$(IntermediateDirectory)/.d:
    @$(MakeDirCommand) "./Debug"

PreBuild:


##
## Objects
##
$(IntermediateDirectory)/main.cpp$(ObjectSuffix): main.cpp $(IntermediateDirectory)/main.cpp$(DependSuffix)
    $(CXX) $(IncludePCH) $(SourceSwitch) "C:/Users/Andre/Documents/CodeLiteWorkspace/testproject/main.cpp" $(CXXFLAGS) $(ObjectSwitch)$(IntermediateDirectory)/main.cpp$(ObjectSuffix) $(IncludePath)
$(IntermediateDirectory)/main.cpp$(DependSuffix): main.cpp
    @$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) -MG -MP -MT$(IntermediateDirectory)/main.cpp$(ObjectSuffix) -MF$(IntermediateDirectory)/main.cpp$(DependSuffix) -MM main.cpp

$(IntermediateDirectory)/main.cpp$(PreprocessSuffix): main.cpp
    $(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) $(PreprocessOnlySwitch) $(OutputSwitch) $(IntermediateDirectory)/main.cpp$(PreprocessSuffix) main.cpp


-include $(IntermediateDirectory)/*$(DependSuffix)
##
## Clean
##
clean:
    $(RM) -r ./Debug/
c++ sdl sdl-2
1个回答
1
投票

你已经为CXXFLAGS添加了链接选项 - 它们不属于 - 但不是LibPathLibsLinkOptions。因此,您在编译阶段指定了链接器选项(由于未执行任何链接,因此不执行任何操作),但在链接阶段没有这些选项。

“逻辑上”正确的设置将是

Libs:=-lmingw32 -lSDL2main -lSDL2
LibPath:=-LC:\SDL2\lib -L.
LinkOptions:=-Wl,-subsystem,windows -mwindows

(不确定SDL2main应该在那里,因为你不清楚你是否使用它,因为你有WinMain而不是main / SDL_main

此外,你真的问-w选项的问题,所以你不应该感到惊讶。如果您不想要额外的问题,请不要阻止编译器帮助您,甚至更好 - 至少要求-Wall提供更多帮助。

© www.soinside.com 2019 - 2024. All rights reserved.