Undefined Reference C++

C error: undefined reference to function, but it IS defined

How are you doing the compiling and linking? You'll need to specify both files, something like:

gcc testpoint.c point.c

...so that it knows to link the functions from both together. With the code as it's written right now, however, you'll then run into the opposite problem: multiple definitions of main. You'll need/want to eliminate one (undoubtedly the one in point.c).

In a larger program, you typically compile and link separately to avoid re-compiling anything that hasn't changed. You normally specify what needs to be done via a makefile, and use make to do the work. In this case you'd have something like this:

OBJS=testpoint.o point.o

testpoint.exe: $(OBJS)
gcc $(OJBS)

The first is just a macro for the names of the object files. You get it expanded with $(OBJS). The second is a rule to tell make 1) that the executable depends on the object files, and 2) telling it how to create the executable when/if it's out of date compared to an object file.

Most versions of make (including the one in MinGW I'm pretty sure) have a built-in "implicit rule" to tell them how to create an object file from a C source file. It normally looks roughly like this:

.c.o:
$(CC) -c $(CFLAGS) $<

This assumes the name of the C compiler is in a macro named CC (implicitly defined like CC=gcc) and allows you to specify any flags you care about in a macro named CFLAGS (e.g., CFLAGS=-O3 to turn on optimization) and $< is a special macro that expands to the name of the source file.

You typically store this in a file named Makefile, and to build your program, you just type make at the command line. It implicitly looks for a file named Makefile, and runs whatever rules it contains.

The good point of this is that make automatically looks at the timestamps on the files, so it will only re-compile the files that have changed since the last time you compiled them (i.e., files where the ".c" file has a more recent time-stamp than the matching ".o" file).

Also note that 1) there are lots of variations in how to use make when it comes to large projects, and 2) there are also lots of alternatives to make. I've only hit on the bare minimum of high points here.

Why does the compiler is giving me this: undefined reference to function?

You can fix this by doing three things:

  1. Don't include a .c file. You're already providing it to gcc on the command line.
  2. Include help.h in files where you use those functions.
  3. Not using inline. You can't use inline when the caller and callee are in different translation units.

main.c:

#include <stdio.h>
// #include "funcs.c"
#include "help.h"

int main()
{
int z = 0;
int wh = 1;
while (wh == 1)
{
printf("What you want?\n1-Plus\n2-Minus\n");
scanf("%d", &z);
if (z == 1)
{
plus();
}
if (z == 2)
{
minus();
}
}
printf("The program ended\n");

return 0;
}

funcs.c

#include <stdio.h>

void plus(void)
{
int a = 0;
int b = 0;
printf("Pls insert a numb\n");
scanf("%d", &a);
printf("Pls insert a numb\n");
scanf("%d", &b);
a = a + b;
printf("The result is: %d\n", a);
}

void minus(void)
{
int a = 0;
int b = 0;
printf("Pls insert a numb\n");
scanf("%d", &a);
printf("Pls insert a numb\n");
scanf("%d", &b);
a = a - b;
printf("The result is: %d\n", a);
}

help.h:

extern int a;
extern int b;
extern int z;
extern int wh;
void minus(void);
void plus(void);

Compile and run like so:

$ gcc -Wall -Werror funcs.c main.c
$ ./a.out
What you want?
1-Plus
2-Minus
^C

Other thoughts:

extern int a;
extern int b;
extern int z;
extern int wh;

You're already declaring these variables locally. This is unneeded. The extern keyword tells the compiler that these variables are defined in another translation unit that it can't see. This isn't true, so you should just remove these.

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

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.

Undefined Reference to (FUNCTIONNAME) in C

Try:

gcc -c SGGINPUT.h SGGINPUT.c

A result file will be an obj file:

SGGINPUT.o

Next compile it this way:

gcc -g SGGINPUT.o test.c -o "Output_name"

Plus you don't really need to include <stdio.h> in your header, you are just declaring your functions there and nothing more.

In C, what constitutes an undefined reference to a symbol?

Including the header file just declares the symbols, it does not define them -- they'll be undefined in your test.c compilation unit because it does not ever define them.

The read symbol is in fact defined in libc.so -- the C standard library -- but when linking your program it first sees your compilation unit (test.o) at which point read is still undefined. So the references to read are instead mapped to __wrap_read.

definitions and declarations are two different things. Related, but different.

**Undefined reference** error while linking two libraries referring to one-another

Libraries must be listed in the order their symbols are needed.

The command cc $(CFLAGS) -o $(TARGET) $(OBJFILES) $(LIBDIRS) -lfunc2 -lfunc1 tells the linker to first use the func2 library to resolve any pending references in the executable it is building and then to use the func1 library.

Since the linker processes func2 first, and, at the time it does so, there is no pending reference to print2, the linker does not include the module with print2 in the executable.

Later, when the linker is processing func1, it includes the module with print1 in the executable because main uses it. That module print1 uses print2, so including that module adds a new reference to print2. Then, when the linker is done processing func1, it has an unresolved reference. The linker does not go back to func2 to check it again.

Since the func1 library depends on func2, change the link command to cc $(CFLAGS) -o $(TARGET) $(OBJFILES) $(LIBDIRS) -lfunc1 -lfunc2.

(If the func2 library also depends on func1, that is a bad design and should be reconsidered. If it is not changed, asking the linker to reconsider the libraries multiple times, as with -lfunc1 -lfunc2 -lfunc1, might fix the immediate problem, but others can arise.)

C++: Undefined Reference to 'CYourClass::Method()'

It looks as though you are doing something like:

g++ dog.cpp -o dog

Without the -c flag (for compile only, don't link) the compiler will attempt to make an executable from that one file.

Generally speaking you need to do one of the following:

  1. Compile each cpp file separately, and then link at the end
# compilation
g++ -c dog.cpp -o dog.o
g++ -c breeds.cpp -o breeds.o
g++ -c main.cpp -o main.o

# now link the 3 object files into the exe
g++ -o myApp main.o dog.o breeds.o
  1. Compile them in one go
g++ -o myApp main.cpp dog.cpp breeds.cpp

  1. Use a makefile
all : myApp

# dog.o depends on dog.cpp & breeds.h. When those change, run line below
dog.o: dog.cpp breeds.h
gcc -c -o dog.o dog.cpp

breeds.o: breeds.cpp dog.h
gcc -c -o breeds.o breeds.cpp

main.o: main.cpp breeds.h dog.h
gcc -c -o main.o dog.cpp

# final app depends on the object files, when they change, recompile.
myApp: main.o dog.o breeds.o
gcc -o myApp main.o dog.o breeds.o

clean:
rm -f *.o

  1. Use some IDE to manage this for you (or use something like cmake)


Related Topics



Leave a reply



Submit