C++ Error: Undefined Reference to 'Main'

Undefined reference to main - collect2: ld returned 1 exit status

It means that es3.c does not define a main function, and you are attempting to create an executable out of it. An executable needs to have an entry point, thereby the linker complains.

To compile only to an object file, use the -c option:

gcc es3.c -c
gcc es3.o main.c -o es3

The above compiles es3.c to an object file, then compiles a file main.c that would contain the main function, and the linker merges es3.o and main.o into an executable called es3.

C Linking Error: undefined reference to 'main'

You should provide output file name after -o option. In your case runexp.o is treated as output file name, not input object file and thus your main function is undefined.

what does (.text+0x20): undefined reference to `main' mean

You're not just separately compiling (-c), you're trying to do a full link but the linker hasn't found any main function.

For a complete link you need to include all your source or object files in your g++ invocation (especially the one with your main function).

For separately compiling just your one file (to be linked later) you need to add the -c option.

Linker error: undefined reference to `main'

It seems that a main function is missing in your program.

#include <cs50.h>
#include <stdio.h>

bool maths (int a, int b, int c)
{
int first = a + b;
int second = b + c;
int third = c + a;
if (first <= c)
{
return false;
}
else if (second <= a)
{
return false;
}
else if (third <= b)
{
return false;
}
else
{
return true;
}
}

int main ()
{
maths(1,2,3);

return 0;
}

C/GTK - Makefile error: undefined reference to main when invoking make

This rule:

sudoku: $(OBJS)
$(CC) -o $(OBJS) $(LIBS)

is wrong, because you have omitted the argument to the -o compiler option. As a result, the first (and in this case, only) file among those designated by $(OBJS) is taken as the name of the output file instead of one to include in the link. You presumably wanted this, instead:

sudoku: $(OBJS)
$(CC) -o $@ $(OBJS) $(LIBS)

($@ expands to the name of the target being built).

Additionally, although this rule will work well enough when there is only one source file:

$(OBJS): $(SRC)
$(CC) -c $(CFLAGS) -o $(OBJS) $(SRC)

, it will break if there are two or more. I would actually remove it altogether, as it does not do anything that make's built-in rule for constructing object files from C source files doesn't also do.

How to solve undefined reference in main function?

After you comment, even if including the cpp files is a workaround, please do not do that!

You have two acceptable ways.

  1. define the method in the class itself in the include file

    Some IDE by default create an include file containing only the method declarations and put the definitions in an implementation cpp file. But the standard allow the definition to be inside the class definition, it is commonly used for small methods or templates (*)

    So your header could be:

     #ifndef PID_H
    #define PID_H
    class PID
    {
    float _kp,_ki,_kd,_error,_previous_error,_dt;
    public:
    void set_params(float kp, float ki, float kd,float error,float previous_error,float dt) {
    _kp = kp;
    _ki = ki;
    _kd = kd;
    _error = error;
    _previous_error = previous_error;
    _dt = dt;
    }

    };

    #endif
  2. add the pid.cpp file to the list of source files for your project.

    This part actually depends on your build tools. But your screenshot shows that you already compile app.cpp and main.cpp. You just have to process pid.cpp the same way.


For templates, the recommended way is to put all the implementation in the include file. Not doing so requires to consistently explicitely instanciate the template for all its future usages, which is not an option in a general library.

makefile: undefined reference to main

Your rule for user_interface.o is wrong. You need to use the -c option to tell it that it's creating an object file rather than an executable, so it doesn't need a main() function.

all: main Server

main: user_interface.o main.o
gcc main.o user_interface.o -o main

main.o: main.c
gcc -c main.c

user_interface.o: user_interface.c
gcc -c user_interface.o

Server: Server.c
gcc Server.c -o Server

clean:
rm -rf main *.o Server

make actually has a built-in rule for compiling .c to .o, so you don't actually need those rules.



Related Topics



Leave a reply



Submit