2
我的項目是要在Linux中使用SDL一些移動精靈一個簡單的小屏幕,但我不希望使用的IDE。我的目標是更好地理解makefile和所有這些業務;然而,我遇到了一堵我無法擺脫的牆。我希望有人能解釋爲什麼我有什麼是錯的,我能做些什麼來避免它!make文件和SDL有多個文件
所以,我的程序有多個依賴那種通過每個文件的級聯的 - 因爲這樣我列爲必要時在Makefile的依賴關係。這裏是我的代碼...
OUT=program
CC=g++
SDL=-lSDLmain -lSDL -lSDL_image -lSDL_ttf
all: constants globals functions sprite sprite_test
${CC} sprite_driver.o sprite.o functions.o globals.o constants.h -o ${OUT} ${SDL}
constants:
${CC} -c constants.h
globals: constants
${CC} -c globals.cpp globals.h constants.h ${SDL}
functions: constants globals
${CC} -c functions.cpp globals.h globals.o constants.h ${SDL}
sprite: constants globals functions
${CC} -c sprite.cpp functions.o globals.o constants.h ${SDL}
sprite_test: constants globals functions sprite
${CC} -c sprite_driver.cpp sprite.o functions.o globals.o constants.h ${SDL}
由編譯器到達功能時,它說,globals.o沒有被正確鏈接。在這個文件裏面有許多外部聲明,這些聲明在編譯後面沒有被識別,所以看起來好像有多個相同變量的聲明。這是控制檯吐出的內容。
g++ -c constants.h
g++ -c globals.cpp globals.h constants.h -lSDLmain -lSDL -lSDL_image -lSDL_ttf
g++ -c functions.cpp globals.h globals.o constants.h -lSDLmain -lSDL -lSDL_image -lSDL_ttf
g++: warning: globals.o: linker input file unused because linking not done
g++ -c sprite.cpp functions.o globals.o constants.h -lSDLmain -lSDL -lSDL_image -lSDL_ttf
g++: warning: functions.o: linker input file unused because linking not done
g++: warning: globals.o: linker input file unused because linking not done
g++ -c sprite_driver.cpp sprite.o functions.o globals.o constants.h -lSDLmain -lSDL -lSDL_image -lSDL_ttf
g++: warning: sprite.o: linker input file unused because linking not done
g++: warning: functions.o: linker input file unused because linking not done
g++: warning: globals.o: linker input file unused because linking not done
g++ sprite_driver.o sprite.o functions.o globals.o constants.h -o program -lSDLmain -lSDL -lSDL_image -lSDL_ttf
sprite.o:(.bss+0x0): multiple definition of `screen'
sprite_driver.o:(.bss+0x0): first defined here
sprite.o:(.bss+0x4): multiple definition of `event'
sprite_driver.o:(.bss+0x4): first defined here
sprite.o:(.bss+0x18): multiple definition of `keystate'
sprite_driver.o:(.bss+0x18): first defined here
sprite.o:(.bss+0x1c): multiple definition of `font'
sprite_driver.o:(.bss+0x1c): first defined here
sprite.o:(.bss+0x20): multiple definition of `t_black'
sprite_driver.o:(.bss+0x20): first defined here
functions.o:(.bss+0x0): multiple definition of `screen'
sprite_driver.o:(.bss+0x0): first defined here
functions.o:(.bss+0x4): multiple definition of `event'
sprite_driver.o:(.bss+0x4): first defined here
functions.o:(.bss+0x18): multiple definition of `keystate'
sprite_driver.o:(.bss+0x18): first defined here
functions.o:(.bss+0x1c): multiple definition of `font'
sprite_driver.o:(.bss+0x1c): first defined here
functions.o:(.bss+0x20): multiple definition of `t_black'
sprite_driver.o:(.bss+0x20): first defined here
collect2: error: ld returned 1 exit status
make: *** [all] Error 1
感謝您的幫助!我用了一個快速的例子,我的一位老師在課堂上遞給我並試圖通過這個例子來學習;顯然無濟於事。謝謝你的解釋,但是,非常感謝。 – fhornplayer