linux服务器自定义安装gcc4.8.2为支持学习C++11,使用到cmake时总是报错如下:
-- The C compiler identification is unknown
-- The CXX compiler identification is GNU 4.8.2
-- Check for working C compiler: /home/**/gcc11/gcc-4.8.2/bin/g++
-- Check for working C compiler: /home/**/gcc11/gcc-4.8.2/bin/g++ -- broken
CMake Error at /usr/local/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:52 (MESSAGE):
The C compiler "/home/**//gcc11/gcc-4.8.2/bin/g++" is not able to
compile a simple test program.
It fails with the following output:
Change Dir: /home/**/study/c++11/thread/threadpool/build/CMakeFiles/CMakeTmp
Run Build Command:/usr/bin/gmake "cmTryCompileExec3281654297/fast"
/usr/bin/gmake -f CMakeFiles/cmTryCompileExec3281654297.dir/build.make
CMakeFiles/cmTryCompileExec3281654297.dir/build
gmake[1]: Entering directory
`/DATA/**/study/c++11/thread/threadpool/build/CMakeFiles/CMakeTmp'
/usr/local/bin/cmake -E cmake_progress_report
/home/**/study/c++11/thread/threadpool/build/CMakeFiles/CMakeTmp/CMakeFiles
1
Building C object
CMakeFiles/cmTryCompileExec3281654297.dir/testCCompiler.c.o
/home/**/gcc11/gcc-4.8.2/bin/g++ -o
CMakeFiles/cmTryCompileExec3281654297.dir/testCCompiler.c.o -c
/home/**/study/c++11/thread/threadpool/build/CMakeFiles/CMakeTmp/testCCompiler.c
/home/**/study/c++11/thread/threadpool/build/CMakeFiles/CMakeTmp/testCCompiler.c:2:3:
error: #error "The CMAKE_C_COMPILER is set to a C++ compiler"
# error "The CMAKE_C_COMPILER is set to a C++ compiler"
^
gmake[1]: Leaving directory
`/DATA/**/study/c++11/thread/threadpool/build/CMakeFiles/CMakeTmp'
gmake[1]: *** [CMakeFiles/cmTryCompileExec3281654297.dir/testCCompiler.c.o]
Error 1
gmake: *** [cmTryCompileExec3281654297/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:3 (project)
-- Configuring incomplete, errors occurred!
如上报错,则是由于编译时找不到对应的gcc的4.8.2的相关文件,就算环境变量中带有gcc4.8.2的路径,也必须在CMakeLists.txt中指出:如下所示,添加这两个set设置,则cmake成功
set(CMAKE_C_COMPILER "/DATA/**/gcc11/gcc-4.8.2/bin/gcc")
set(CMAKE_CXX_COMPILER "/DATA/**/gcc11/gcc-4.8.2/bin/g++")
完整CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
set(CMAKE_C_COMPILER "/DATA/**/gcc11/gcc-4.8.2/bin/gcc")
set(CMAKE_CXX_COMPILER "/DATA/**/gcc11/gcc-4.8.2/bin/g++")
project(demo)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
aux_source_directory(. DIR_SRCS)
add_executable(11ThreadPool ${DIR_SRCS})
修改上述内容,即可用。