2016-01-23 57 views
0

我正在練習信號,並且我已經從gcc移到eclipse上,並且遇到了一些問題。 「:分配給從類型鍵入‘聯盟’‘無效(*)(INT)’時不兼容類型的錯誤」爲eclipse配置C99 for posix

尋找網上我看到,因爲編譯器錯誤的機率低於GCC代碼編譯罰款月食我得到錯誤 使用pre C99版本。於是,我就使Eclipse編譯C99版,我發現下面的鏈接How do you configure GCC in Eclipse to use C99?

我試圖做出改變,如SO鏈接建議,但目前我的其他標誌具有線,說 「-c -fmessage長度= 0 「

如果我添加-std = C99作爲之前或之後行編譯器無法找到文件本身

我假設我使用預C99版得到,因爲編譯器的錯誤郵寄建議。請糾正我,如果我以錯誤的方向,如果它的正確我追什麼會是-std = C99添加到標誌選項,以使Eclipse中使用C99的正確方法

編輯:基於答案時,我改變了sig_handler參數我能夠在不添加-std = c99標誌的情況下進行編譯,但是按照建議,我會遇到編譯錯誤。下面是編譯行

GCC -O0 -g3 -Wall -c -fmessage長度= 0 -std = C99 -MMD -MP -MF

`odd_process.d" -MT"odd_process.d" -o "odd_process.o" "../odd_process.c`" 
    ../odd_process.c: In function ‘main’: 
    ../odd_process.c:13:2: error: unknown type name ‘sigset_t’ 
    ../odd_process.c:14:19: error: storage size of ‘sa’ isn’t know 

Ñ

#include <stdio.h> 
#include <stdlib.h> 
#include <signal.h> 

void sighandler(int sig) 
{ 
    printf("signal has been caught \n "); 

} 

int main(void) 
{ 
    sigset_t block_signal, empty_signal; 
    struct sigaction sa; 

    pid_t childid; 
    int i; 

    sigemptyset(&sa.sa_mask); 
    sa.sa_flags = 0; 

    **/*for below line while running on eclipse I see error which says*/ 
    /***error: incompatible types when assigning to type ‘union <anonymous>’ from type ‘void (*)(int)’** */** 

**sa.__sigaction_handler = sighandler;** 

    stdbuf(stdout, NULL); 

    if (sigaction(SIGCHLD, &sa, NULL) == -1) 
    { 
     printf("value not passed to signal handler \n "); 
    } 

    sigemptyset(&block_signal); 
    sigaddset(&block_signal, SIGCHLD); 

    if (sigprocmask(SIG_SETMASK, &block_signal, NULL) == -1) 
    { 
     printf("error occurred while setting signal mask \n"); 
    } 

    childid = fork(); 

    printf("value of child id is -- %d ", childid); 

    switch(childid) 
    { 
     case -1: 
      printf("Error condition child creation did not happen properly \n"); 
      exit(-1); 
     case 0: 
      printf(" Child got created and will print odd number !!! "); 
      sleep(5); 
      exit(1); 
     default: 
      printf(" parent gets created !!! \n "); 
      break; 
    } 

    sigemptyset(&empty_signal); 
    sigsuspend(&empty_signal); 
    printf(" parent will print \n "); 

    for(i = 0; i < 10; i++) 
    { 
     printf("%d ", i); 
    } 

    printf("\n"); 

    return EXIT_SUCCESS; 
} 
+0

張貼代碼丟失的#include''關於 – user3629249

+0

這行做:'stdbuf(標準輸出,NULL );'我懷疑你真的想用'setbuf()' – user3629249

+0

這一行:'sa.sigaction_handler = sighandler;'應該寫成:'sa.sa_handler = sighandler; – user3629249

回答

1

修改CFLAGS,(與所需的項目打開)。

  1. 點擊菜單項project
  2. 單擊下拉菜單項properties
  3. 單擊左側窗口中的C/C++Build標籤 - 擴大
  4. 請單擊展開的C/C++Build
  5. Settings選項卡中單擊GCC C Comiler選項卡中的第二列 - 擴大
  6. 點擊miscellaneous選項卡中第二列選擇第三列
  7. 選擇「其他flags`線
  8. 進入次è附加的標誌,如-std=gnu99
  9. 點擊ok

這可能需要爲每個項目

+0

對不起,延遲迴復 - 我可以通過更改\t //sa.__sigaction_handler = sighandler;而無需添加-std = c99而無錯地進行編譯sa.sa_handler = sighandler;正如我所建議的,我總是在編寫sa時使用gcc編寫sa.sa_handler = sighandler,但是在eclipse上編寫sa。成員函數的選項只顯示了__sigaction_handler,所以我選擇了它,這樣就可以編譯 – oneday

+0

- 關於建議的步驟是的,我已經嘗試過它們,正如我在帖子中指出的那樣,當我輸入附加標誌時,它不會編譯 – oneday

+1

發生這種情況是因爲您使用的是非C99功能s和類型 - 你使用的是posix。你可以使用-std = gnu99而不是-std = c99,它可以讓你訪問包括posix在內的所有GNU擴展,或者添加#define _POSIX_C_SOURCE = 200809L – nos