簡體   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