目錄:
-
用CMake構建SDL時報錯
-
Gcc添加鏈接庫
-
Gcc找不到入口(WinMain)
-
讓SDL啟動時不帶控制臺視窗
用CMake構建SDL時報錯
root@ubuntu:~/SDL# cmake ..
CMake Error at CMakeLists.txt:2 (message):
Prevented in-tree built. Please create a build directory outside of the SDL source code and call cmake from there
這個錯誤資訊翻譯成人話就是:你媽的,別在根目錄里構建專案!快去創建個build目錄,在那里構建!

解決方法很簡單,照它說的做,
root@ubuntu:~/SDL# mkdir build
root@ubuntu:~/SDL# cd build
root@ubuntu:~/SDL/build# cmake ..
CMake Error at CMakeLists.txt:2 (message):
Prevented in-tree built. Please create a build directory outside of the SDL source code and call cmake from there

我們切回專案根目錄,看看目錄里多了什么:
root@ubuntu:~/SDL# ls
# 啊啊啊,檔案太多了,只寫多出來的吧
CMakeFiles
CMakeCache.txt
多出來的一個目錄和txt,就是我們第一次在根目錄構建時創建的Cache,刪掉之后才能在其他位置構建
root@ubuntu:~/SDL# rm -rf CMakeFiles
root@ubuntu:~/SDL# rm CMakeCache.txt
root@ubuntu:~/SDL# cd build
root@ubuntu:~/SDL/build# cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
...
成功!
Gcc添加鏈接庫
草,突然想起Linux沒裝圖形界面,下面都是在Windows下配置的,其實在Linux下也一樣(使用Gcc編譯器),
筆記本太爛,就沒裝IDE,下面就介紹一下Gcc咋添加鏈接庫吧(動態和靜態)
-
元件
這個比較方便,編譯時直接帶上.dll就行
gcc Source.c SDL2d.dll -o Binary -
靜態鏈接庫
考慮到對Linux讀者的兼容性,這里只介紹一種方法:編譯時規定靜態庫位置
gcc -L靜態庫位置 -lSDL2maind -lSDL2d這里要注意一些問題,SDL2maind要寫在SDL2d前面,-l是小寫的L,不是大寫的i
Gcc找不到入口(WinMain)
undefined reference to `WinMain'
這個問題就比較奇特了,在SDL的SDL_main.h中,有這么一行代碼:
#define main SDL_main
它把main替換成了SDL_main,導致gcc找不到程式入口,我找到了兩種解決方法:
-
解除預定義的替換
#undef main -
在gcc中規定入口點
gcc Source.c SDL2d.dll -nostartfile -e SDL_main
第二種方法先讓gcc不使用標準的啟動檔案(main),然后規定入口函式為SDL_main
第二種方法運行結束后程式貌似不會被銷毀...
讓SDL啟動時不帶控制臺視窗
這東西挺煩的,好像只有Windows下有這個問題,使用IDE的同學可以直接在專案設定中關閉,
用微軟家編譯器的可以在代碼中插入一行:
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
這是我在別人博客里看到的,因為我沒裝vc,也不知道能不能用
用Gcc編譯器的同學就要麻煩一點了,需要在編譯時寫成這樣:
gcc -Wl,subsystem,windows Source.c SDL2d.dll -nostartfile -e SDL_main
注意:-Wl,后面的是小寫的L,不是大寫的i!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/43019.html
標籤:C
上一篇:回文數題解
下一篇:C語言實作簡單計算器小專案
