繁体   English   中英

为什么在使用SDL2时仍然出现“未定义参考”链接器错误?

[英]Why am I still getting 'undefined reference' linker errors when using SDL2?

我已经看过堆栈溢出中与该错误直接相关的所有其他页面。

我正在尽我所能,但是仍然出现此错误

未定义对“ 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位二进制文​​件。 我发现它给了我同样的错误,但是如果我要使用普通的int main()入口函数,它会显示WinMain而不是SDL_Init ,所以现在我使用的是int WinMain() ,它消除了该错误。 所以现在我的代码只包含

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

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

如果我注释掉SDL_Init函数,则该程序没有构建错误,但是不执行任何操作。

有谁知道这与我使用的编译器稍有不同吗? 另外,我不确定,但是从构建日志来看,它似乎实际上并未使用我提供的构建选项(我使用的是IDE,因此程序可以对其进行管理)。 最后,如果有人知道为什么要我使用WinMain而不是常规main函数,并告诉我要进行哪些更改以解决此问题,将不胜感激。

生成文件:

##
## 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/

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

“逻辑上”正确的设置是

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

(不确定是否使用SDL2main,因为您使用的是WinMain而不是main / SDL_main ,尚不清楚是否使用它)

另外,您实际上是在询问-w选项的问题,因此不要感到惊讶。 如果您不希望出现其他问题,请不要阻止编译器为您提供帮助,甚至更好-至少-Wall寻求更多帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM