2013-02-24 53 views
1

我有一個簡單的C++測試項目,並寫下我的CMakeLists.txt文件如下;如何正確設置CMakeLists.txt文件?

cmake_minimum_required(VERSION 2.8) 

set(CMAKE_C_COMPILER "C:/MinGW/bin/gcc") 
set(CMAKE_CXX_COMPILER "C:/MinGW/bin/g++") 

project(simpleTest) 
add_executable(main main.cpp) 

當我嘗試運行CMake的GUI的一套發生器MinGW的我收到以下錯誤信息:

The C compiler identification is GNU 4.6.1 
The CXX compiler identification is GNU 4.6.1 
Check for working C compiler: C:/MinGW/bin/gcc 
CMake Error: your C compiler: "C:/MinGW/bin/gcc" was not found. Please set CMAKE_C_COMPILER to a valid compiler path or name. 
CMake Error: Internal CMake error, TryCompile configure of cmake failed 
Check for working C compiler: C:/MinGW/bin/gcc -- broken 
CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:61 (message): 
    The C compiler "C:/MinGW/bin/gcc" is not able to compile a simple test 
    program. 

    It fails with the following output: 



    CMake will not be able to correctly generate this project. 
Call Stack (most recent call first): 
    CMakeLists.txt:7 (project) 


CMake Error: your C compiler: "C:/MinGW/bin/gcc" was not found. Please set CMAKE_C_COMPILER to a valid compiler path or name. 
CMake Error: your CXX compiler: "C:/MinGW/bin/g++" was not found. Please set CMAKE_CXX_COMPILER to a valid compiler path or name. 
Configuring incomplete, errors occurred! 

我在Windows 7 64位和我確認了CMD是什麼; G ++ --version給出G ++(GCC)4.6.1。

我在做什麼錯?

回答

2

很抱歉,這看起來像編譯器沒有安裝在您指定的位置。另請參閱here爲什麼你應該避免設置編譯器的CMakeLists.txt

所以我刪除這些,清除緩存的CMake,CMake的調用之前設置環境變量CC和CXX,然後再試一次。

+0

你是對的沒有在CMakeLists.txt中設置編譯器,但一再檢查編譯器安裝在我指定的地方。 – Amani 2013-02-24 16:03:10

+0

您是否刪除了兩行並清除了cmake緩存?此外,我添加到我的答案,你應該嘗試設置環境變量,然後再調用cmake – 2013-02-24 16:15:24

1

我認爲你的問題在於路徑字符串。試試這個:

set(CMAKE_C_COMPILER "C:\\MinGW\\bin\\gcc") 
set(CMAKE_CXX_COMPILER "C:\\MinGW\\bin\\g++") 

或者,你也可以在CMake GUI中設置你的編譯器。點擊生成後,選擇「指定本地編譯器」選項並設置或瀏覽到正確的位置。

+1

問題(因爲我發現和從其他建議)不是你指出的,而是事實sh.exe是在我的系統路徑。這也可以通過以下事實來證明:如果我將生成器設置爲「MSYS Makefiles」,即使沒有在CMakeLists.txt文件中設置CMAKE_C_COMPILER或CMAKE_CXX_COMPiLER,它也可以工作。 – Amani 2013-02-26 15:56:15