ricothebrol
ricothebrol

Reputation: 81

GCC : weird errors with -O0, none without

I'd like to compile my C code without optimisations, but when I add the -O0 option to the compilation command I get lots of "multiple definitions" errors, of functions I didn't even write ! I guess there's something obvious I'm missing here but as a noob I just can't figure out what...

Here's my code :

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <wiringPi.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <semaphore.h>
#include <unistd.h>
#include <sched.h>
#include <time.h>

#define N       1800

unsigned long int tsec, tnsec;

int main(void)
{
        int err, i;
        printf("started\n");

        struct timespec tps, tpe, req, rem;

        req.tv_nsec = 400000;
        req.tv_sec = 0;

        struct sched_param param;
        param.sched_priority = 99;
        err = sched_setscheduler(getpid(), SCHED_FIFO, &param);
        if (err != 0)
                printf("sched_setscheduler error");
        err = sched_getparam(getpid(), &param);
        if (err != 0)
                printf("sched_getparam error");
        printf("priority = %i\n", param.sched_priority);

        int policy = sched_getscheduler(getpid());
        printf("policy = %i\n", policy);


        sleep(1);

        clock_gettime(CLOCK_REALTIME, &tps);

        #include "noppynop2.c"
        #include "noppynop2.c"
        #include "noppynop2.c"
        #include "noppynop2.c"
        #include "noppynop2.c"
        #include "noppynop2.c"


        clock_gettime(CLOCK_REALTIME, &tpe);
        tnsec = tpe.tv_nsec - tps.tv_nsec;
        tsec = tpe.tv_sec - tps.tv_sec;
        printf("time = %lu sec %lu nsec\n", tsec, tnsec);


        printf("finished\n");

        return 0;
}

To sum thing up, it's a bruteforce attempt to create a delay on a real-time kernel's Linux. "noppynop2.c" contains 500 times asm("nop"). I don't think this is the problem here but that's the context. The code is mostly just a test, and I want to get rid of all optimisations because for the moment the compiler tends to remove an (apparently) unpredictable quantity of "nops", which is the exact opposite of what I'm trying to do...

When I compile without the -O0 option, everything goes fine :

pi@raspberrypi ~/Desktop/Projects/test_timemeasure_sched $ gcc -Wall -o test_time_measure_sched main.c -lwiringPi -lpthread -lrt
main.c: In function ‘main’:
main.c:22:33: warning: unused variable ‘rem’ [-Wunused-variable]
main.c:22:28: warning: variable ‘req’ set but not used [-Wunused-but-set-variable]
main.c:19:11: warning: unused variable ‘i’ [-Wunused-variable]

But when I add the -O0 here's what I get :

pi@raspberrypi ~/Desktop/Projects/test_timemeasure_sched $ gcc -Wall -o -O0 test_time_measure_sched main.c -lwiringPi -lpthread -lrt
main.c: In function ‘main’:
main.c:22:33: warning: unused variable ‘rem’ [-Wunused-variable]
main.c:22:28: warning: variable ‘req’ set but not used [-Wunused-but-set-variable]
main.c:19:11: warning: unused variable ‘i’ [-Wunused-variable]
test_time_measure_sched: In function `_fini':
:(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../arm-linux-gnueabihf/crti.o:(.fini+0x0): first defined here
test_time_measure_sched: In function `__data_start':
:(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../arm-linux-gnueabihf/crt1.o:(.data+0x0): first defined here
test_time_measure_sched: In function `__data_start':
:(.data+0x4): multiple definition of `__dso_handle'
/usr/lib/gcc/arm-linux-gnueabihf/4.6/crtbegin.o:(.data+0x0): first defined here
test_time_measure_sched:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../arm-linux-gnueabihf/crt1.o:(.rodata.cst4+0x0): first defined here
test_time_measure_sched: In function `_start':
:(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../arm-linux-gnueabihf/crt1.o:(.text+0x0): first defined here
test_time_measure_sched: In function `_init':
:(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../arm-linux-gnueabihf/crti.o:(.init+0x0): first defined here
/tmp/cc5At86c.o: In function `main':
main.c:(.text+0x0): multiple definition of `main'
test_time_measure_sched::(.text+0xac): first defined here
collect2: ld returned 1 exit status

What could I do to get rid of those errors ?

Thanx in advance for your help.

Best regards.

Eric

Upvotes: 1

Views: 392

Answers (1)

Sister Fister
Sister Fister

Reputation: 368

The gcc command should be

gcc -Wall -O0 -o test_time_measure_sched main.c -lwiringPi -lpthread -lrt

instead of

gcc -Wall -o -O0 test_time_measure_sched main.c -lwiringPi -lpthread -lrt

With the second command the old binary test_time_measure_sched is treated as an input file by gcc. The additional errors you are getting are all linker errors, which indicates that gcc tries to link the old binary to your new code.

Upvotes: 7

Related Questions