我有以下c文件。
prog1.h
#ifndef PROG1_H
#define PROG1_H
extern char* hello;
#endif
prog1.c
#include "prog1.h"
char *hello="hello";
one.c
#include "prog1.h"
#include <stdio.h>
int main(){
printf("%s", hello);
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
project(tester)
add_executable(main one.c)
add_executable(prog1 prog1.c)
尝试在KDevelop中构建项目“测试器”时,得到以下输出。
output log generated in KDevelop on building the project
但是当我通过命令在终端上显式编译文件时-
gcc one.c prog1.c -o输出文件
它返回运行时的“输出文件”-
./ outputfile
我得到了期望的输出
akash@Z50-70:~/projects/Tester$ gcc one.c prog1.c -o outputfile
akash@Z50-70:~/projects/Tester$ ./outputfile
hello
akash@Z50-70:~/projects/Tester$
有人试图在KDevelop中构建项目时遇到我遇到的问题吗?
答案 0 :(得分:1)
这与KDevelop无关。它与您的CMakeLists.txt
有关。 cmake
在KDevelop和CLion中用作项目经理。但是您的问题并不特定于IDE。
该错误表明未定义对main的引用,因为您正在将prog1.c
编译为可执行文件。可执行文件应具有主要功能。 prog1.c
没有一个。
您要添加两个不同的可执行文件。在单个可执行文件中添加两个源。
add_executable(main one.c prog1.c)
当然,您可以在一个项目中拥有多个可执行文件。但是,通常除非您有特殊要求,否则仅在仅生成单个可执行文件的情况下,将项目名称设置为可执行文件名称。
add_executable(tester one.c prog1.c)