0

I have the following c files.

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)

On attempt of building the project "tester" in KDevelop I get the following output.

output log generated in KDevelop on building the project

But when I compile the file explicitly on terminal by the command -

gcc one.c prog1.c -o outputfile

it returns me the "outputfile" which on running -

./outputfile

i get the desired output

akash@Z50-70:~/projects/Tester$ gcc one.c prog1.c -o outputfile
akash@Z50-70:~/projects/Tester$ ./outputfile
hello
akash@Z50-70:~/projects/Tester$ 

Can anyone help me out with the issue i am facing while trying to build the project in KDevelop?

Neel Basu
  • 12,638
  • 12
  • 82
  • 146

1 Answers1

1

This is not something to do with KDevelop. It is about your CMakeLists.txt. cmake is used as the project manager in KDevelop and CLion. But your question is not specific to an IDE.

The error says undefined reference to main because you are compiling prog1.c as an executable. and an executable should have a main function. prog1.c doesn't have one.

You are adding two different executables. add two sources in a single executable.

add_executable(main one.c prog1.c)

Surely you can have multiple executables in a single project. However in general the project name is set as the executable name if you are producing a single executable only unless you have a special requirement.

add_executable(tester one.c prog1.c)
Neel Basu
  • 12,638
  • 12
  • 82
  • 146