GCC:奇怪的错误 -O0,none 没有
GCC : weird errors with -O0, none without
我想在没有优化的情况下编译我的 C 代码,但是当我将 -O0 选项添加到编译命令时,我得到了很多 "multiple definitions" 错误,我什至没有编写函数!我想我在这里遗漏了一些明显的东西,但作为一个菜鸟,我就是想不通是什么……
这是我的代码:
#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, ¶m);
if (err != 0)
printf("sched_setscheduler error");
err = sched_getparam(getpid(), ¶m);
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;
}
总而言之,这是在实时内核 Linux 上创建延迟的蛮力尝试。 "noppynop2.c" 包含 500 次 asm("nop")。我不认为这是这里的问题,但这就是上下文。该代码主要只是一个测试,我想摆脱所有优化,因为目前编译器倾向于删除(显然)不可预测的 "nops" 数量,这与我正在尝试的完全相反做...
当我在没有 -O0 选项的情况下编译时,一切正常:
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]
但是当我添加 -O0 时,我得到的是:
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
我该怎么做才能消除这些错误?
在此先感谢您的帮助。
此致。
埃里克
gcc 命令应该是
gcc -Wall -O0 -o test_time_measure_sched main.c -lwiringPi -lpthread -lrt
而不是
gcc -Wall -o -O0 test_time_measure_sched main.c -lwiringPi -lpthread -lrt
使用第二个命令,旧二进制文件 test_time_measure_sched
被 gcc 视为输入文件。您得到的其他错误都是 linker 错误,这表明 gcc 试图 link 旧二进制文件到您的新代码。
我想在没有优化的情况下编译我的 C 代码,但是当我将 -O0 选项添加到编译命令时,我得到了很多 "multiple definitions" 错误,我什至没有编写函数!我想我在这里遗漏了一些明显的东西,但作为一个菜鸟,我就是想不通是什么……
这是我的代码:
#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, ¶m);
if (err != 0)
printf("sched_setscheduler error");
err = sched_getparam(getpid(), ¶m);
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;
}
总而言之,这是在实时内核 Linux 上创建延迟的蛮力尝试。 "noppynop2.c" 包含 500 次 asm("nop")。我不认为这是这里的问题,但这就是上下文。该代码主要只是一个测试,我想摆脱所有优化,因为目前编译器倾向于删除(显然)不可预测的 "nops" 数量,这与我正在尝试的完全相反做...
当我在没有 -O0 选项的情况下编译时,一切正常:
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]
但是当我添加 -O0 时,我得到的是:
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
我该怎么做才能消除这些错误?
在此先感谢您的帮助。
此致。
埃里克
gcc 命令应该是
gcc -Wall -O0 -o test_time_measure_sched main.c -lwiringPi -lpthread -lrt
而不是
gcc -Wall -o -O0 test_time_measure_sched main.c -lwiringPi -lpthread -lrt
使用第二个命令,旧二进制文件 test_time_measure_sched
被 gcc 视为输入文件。您得到的其他错误都是 linker 错误,这表明 gcc 试图 link 旧二进制文件到您的新代码。