2017-11-07 8 views
1

コンパイルコマンドに-O0オプションを追加すると、関数I書くことさえしなかった!GCC:奇妙なエラー-O0なしなし

#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; 
} 

事までを要約すると、それはへのブルートフォースの試みです:私はここに

は私のコードです...そこに私はここに欠けている明白なものだけどnoobのように私はちょうど何を把握することはできません推測しますリアルタイムカーネルのLinuxに遅延を生じさせます。 "noppynop2.c"には500回のasm( "nop")が含まれています。私はこれがここの問題だとは思わないが、それは文脈だ。コードはたいていは単なるテストであり、コンパイラは予測できない量の "nops"を取り除く傾向があるので、私はすべての最適化を取り除きたいと思っています。私は-O0オプションなしでコンパイルすると...

、すべてがうまく行く:

[email protected] ~/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を追加するときに、ここで私が何を得るのです。

[email protected] ~/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 

私は何ができますそれらのエラーを取り除くには?

あなたの助けを借りて前もってThanx。

よろしくお願いいたします。

エリック

+4

'-o'オプションの場所が間違っています。 '-o test_time_measure_sched'をまとめてください。 –

+2

'' noppynop2.c "は500回のasm(" nop ")'を含んでいます - それには* all *が含まれていますか?あなたはそれのスニペットを投稿できますか? –

+0

@JohnBode:はい、それは500倍のasm( "nop")しか含みません、それ以上のものはありません; – ricothebrol

答えて

7

GCCコマンドは、古いバイナリtest_time_measure_schedがgccで入力ファイルとして扱われ、2番目のコマンドで

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 

であるべきです。追加のエラーはすべてリンカーエラーです.gccが古いバイナリを新しいコードにリンクしようとしています。

+3

Quibble:gccは明らかに古いバイナリをCコードとして扱っていません。そうしようとすると、大量のコンパイルエラーが確実に報告されます。代わりに、リンクステージで失敗します。しかし古いバイナリを新しいバイナリにリンクすることは非常にうまくいくかもしれません。これは、それが放出するいくつかのリンクエラーと完全に一致します。 –

+1

@JohnBollingerうーん、あなたの右のように見える... –

+0

-oと-O0の間の場所の切り替えは、問題を解決し、非常に人々に感謝! 私はまだ私のasm( "nop")を最適化しましたが、それは別の話題になります! – ricothebrol