arze ramade
arze ramade

Reputation: 317

undefined reference to main issue

I have written a c code file called "utilfunc.c" this code contains my functions that I will use through my code.

Now when I am compiling my "utilfunc.c" file an error happened telling me that undefined reference to 'main' error message . But on this code I don't want the main . What I want is just implementations for my functions.

Can anybody tell me what is happening here?

here is my code :

include "prodcon.h"

static void signalHandler(int signo)
{
    signalflag = 1; // Set the signal flag
}

void initializeWait(void)
{
    /* Link handler functions */
    if (signal(SIGUSR1, signalHandler) == SIG_ERR)
    {
        perror("signal");
        exit(EXIT_FAILURE);
    }
    if (signal(SIGUSR2, signalHandler) == SIG_ERR)
    {
        perror("signal");
        exit(EXIT_FAILURE);
    }

    /* Clear masks */
    sigemptyset(&nomask);
    sigemptyset(&newmask);

    /* Add signals to mask sets */
    sigaddset(&newmask, SIGUSR1);
    sigaddset(&newmask, SIGUSR2);

    /* Block SIGUSR1 and SIGUSR2, and save current signal mask. */
    if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
    {
        perror("sigprocmask");
        exit(EXIT_FAILURE);
    }
}

void signalParent(pid_t pid)
{
    kill(pid, SIGUSR2); // Child tells parent that it is done
}   

void waitParent(void)
{
    while (signalflag == 0)
    {
        sigsuspend(&nomask);    // Wait for signal from parent
    }

    signalflag = 0; // Clear the singalflag

    /* Restore the signal mask to old value */
    if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
    {
        perror("sigprocmask");

    }
}

Upvotes: 2

Views: 2522

Answers (3)

ederag
ederag

Reputation: 2509

This was not the issue for the original poster, but here is an unusual cause for the same error message: option with missing path.

gcc -I../include -O -I. -g -O2 -DNDEBUG -O3 -fPIC -I -c graph.c -o graph.o
/usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../lib64/crt1.o: In function `_start':
/home/abuild/rpmbuild/BUILD/glibc-2.19/csu/../sysdeps/x86_64/start.S:118: undefined reference to `main'
collect2: error: ld returned 1 exit status

Notice the spurious -I before -c graph.c. Without it it compiles just fine

gcc -I../include -O -I. -g -O2 -DNDEBUG -O3 -fPIC -c graph.c -o graph.o

The problem is that the path is missing (-I<path>). Adding the cwd as a path (-I.) works fine for instance

gcc -I../include -O -I. -g -O2 -DNDEBUG -O3 -fPIC -I. -c graph.c -o graph.o

Any missing path after any command can yield this error; for instance with -L.

gcc -I../include -O -I. -g -O2 -DNDEBUG -O3 -fPIC -L -c graph.c -o graph.o
/usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../lib64/crt1.o: In function `_start':
/home/abuild/rpmbuild/BUILD/glibc-2.19/csu/../sysdeps/x86_64/start.S:118: undefined reference to `main'
collect2: error: ld returned 1 exit status

Upvotes: 1

Scott Hunter
Scott Hunter

Reputation: 49920

Depending on the options you give the compiler (you didn't show any, so I can't say specifically about yours), the compiler/linker will try to build a whole application, in which case it would need main defined. But you can provide different options to tell it to just compile the file given, and stop there, which sounds like what you want.

Upvotes: 2

Kerrek SB
Kerrek SB

Reputation: 477670

Add -c to your compiler invocation, so that you only compile the translation unit but do not link it to a complete program:

gcc -c -o foo.o foo.c

Upvotes: 9

Related Questions