Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C programming, what is `undefined reference`error, when compiling? [duplicate]

I have this following simple program I am trying to compile in linux ubuntu.

Main.c:

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

int main()
{   
    struct Person1 p1 = Person1_Constructor(10, 1000);
}

Person.c:

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

struct Person1 Person1_Constructor(const int age, const int salary)
{
    struct Person1 p;
    p.age = age;
    p.salary = salary;
    return p;
};

Person.h:

struct Person1
{
    int age, salary;
};
struct Person1 Person1_Constructor(const int age, const int salary);

Why do I get the following error ?

/tmp/ccCGDJ1k.o: In function `main':
Main.c:(.text+0x2a): undefined reference to `Person1_Constructor'
collect2: error: ld returned 1 exit status

I am using gcc Main.c -o Main to compile.

like image 856
dimitris93 Avatar asked May 02 '15 04:05

dimitris93


People also ask

How do you fix undefined reference error in C?

When we compile these files separately, the first file gives “undefined reference” for the print function, while the second file gives “undefined reference” for the main function. The way to resolve this error is to compile both the files simultaneously (For example, by using g++).

What is undefined reference to printf in C?

This error is often generated because you have typed the name of a function or variable incorrectly. For example, the following code: #include <stdio.h> void print_hello() { printf ("Hello!\n"); } /* To shorten example, not using argp */ int main() { Print_hello(); return 0; }

What are compile errors in C?

Compilation error refers to a state when a compiler fails to compile a piece of computer program source code, either due to errors in the code, or, more unusually, due to errors in the compiler itself.

What is undefined reference to WinMain?

The error means that the compiler try to find WinMain() but it didn't. Either you didn't declare int main() Or your project type is something other than Console. ( Normally Win32GUI)


2 Answers

When you link the program you need to give both Main.o and Person.o as inputs.

Build usually is done in two steps (1) compilation and (2) linking. To compile your sources do:

$ gcc -c -o Main.o Main.c
$ gcc -c -o Person.o Person.c

Or in one line:

$ gcc -c Main.c Person.c

Then the resulting object files must be linked into a single executable:

$ gcc -o Main Main.o Person.o

For small projects, of a few compilation units like yours, both step can be done in one gcc invocation:

$ gcc -o Main Main.c Person.c

both files must be given because some symbols in Person.c are used by Main.c.

For bigger projects, the two step process allows to compile only what changed during the generation of the executable. Usually this is done through a Makefile.

like image 193
Diego Avatar answered Oct 20 '22 05:10

Diego


The problem is probably occurring because you're compiling only Main.c when you need to be compiling Main.c and Person.c at the same time. Try compiling it as gcc Main.c Person.c -o MyProgram.

Whenever you have multiple files, they all need to be specified when compiling in order to resolve external references, as is the case here. The compiler will spit out object files that will later be linked into an executable. Check out this tutorial on makefiles: Make File Tutorial It helps to automate this process.

like image 35
Mlagma Avatar answered Oct 20 '22 04:10

Mlagma