无法编译C程序

时间:2015-10-30 15:53:14

标签: c gcc compilation chromium clion

我正在尝试用Clion编译一个非常简单的(" hello world!" like)C程序,但我一直都在失败。

这是我的代码:

 #include "main.h"
 #include <stdio.h>

 int main() {
 printf("hi hi hi\n");
 return 0;
 }

main.h:

 #ifndef EXONE_HELLO_H
 #define EXONE_HELLO_H

 #endif //EXONE_HELLO_H

和我的make文件:

 cmake_minimum_required(VERSION 3.3)
 project(exone)

 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ­-Wextra ­-Wall ­-Wvla​ -­std=c99")

 set(SOURCE_FILES main.c main.h)
 add_executable(exone ${SOURCE_FILES})

但是我收到了这条消息:

/dist/local/x86_64.debian64-5776/jetbrains/clion-1.1/bin/cmake/bin/cmake

--build /tmp/.clion-oarzi/system/cmake/generated/48ee084e/48ee084e/Debug      
--target exone -- -j 4

[ 50%] Building C object CMakeFiles/exone.dir/main.c.o 
cc: error: -Wextra: No such file or directory 
cc: error: -­Wall: No such file or directory 
cc: error: ­-Wvla​: No such file or directory 
cc: error: -std=c99: No such file or directory
CMakeFiles/exone.dir/build.make:62: recipe for target
'CMakeFiles/exone.dir/main.c.o' failed make[3]: ***
[CMakeFiles/exone.dir/main.c.o] Error 1 CMakeFiles/Makefile2:67:
recipe for target 'CMakeFiles/exone.dir/all' failed make[2]: ***
[CMakeFiles/exone.dir/all] Error 2 CMakeFiles/Makefile2:79: recipe for
target 'CMakeFiles/exone.dir/rule' failed make[1]: ***
[CMakeFiles/exone.dir/rule] Error 2 Makefile:118: recipe for target
'exone' failed make: *** [exone] Error 2

另外,当我使用控制台和gcc编译时,会发生这种情况 将CMAKE_C_FLAGS更改为CMAKE_CXX_FLAGS一切正常(即使我 只在两个地方的第一个地方改变。)

更新: 看来我使用减号而不是破折号。现在我只得到一个CC错误:

`cc: error: unrecognized command line option ‘-Wvla​’`

1 个答案:

答案 0 :(得分:1)

在变量SOURCE_FILES中,您不需要头文件,因为它将由编译器找到。你还需要qoutes才能工作。

set(SOURCE_FILES "main.c")
add_executable(exone "${SOURCE_FILES}")

此外,您需要为所有编译器选项设置带有破折号的编译器选项(您错过了-Wextra

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra -Wall -Wvla -std=c99")

现在你的CMakeLists.txt应该是这样的

cmake_minimum_required(VERSION 3.3)
project(exone)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra -Wall -Wvla -std=c99")

set(SOURCE_FILES "main.c")
add_executable(exone "${SOURCE_FILES}")