在CentOS中編譯c源文件,遇到(.text+0x20): undefined reference to `main'

這是我的bat文件

gcc -c ./shared/108/io.c -o io-1.o 2>>error-1.txt
gcc -I ./shared/108 -c ./000/082/975/CWE190_Integer_Overflow__int_connect_socket_add_02.c -o io-2.o 2>>error-2.txt
gcc -I ./shared/108  -g io-2.o io-1.o   2>>error-3.txt

第一步和第二步都沒有出錯,第三步就報錯:

/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status

也求教了很多人,有人問我是不是根本就沒有寫main()函數,這也就尷尬了,我真的寫了,怎麼就找不到。

c源文件,部分代碼如下:

/* Below is the main(). It is only used when building this testcase on

   its own for testing or for building a binary to use in testing binary

   analysis tools. It is not used when compiling all the testcases as one

   application, which is how source code analysis tools are tested. */

#ifdef INCLUDEMAIN

main(int argc, char * argv[])
{
    /* seed randomness */
    srand( (unsigned)time(NULL) );
#ifndef OMITGOOD
    printLine("Calling good()...");
    CWE190_Integer_Overflow__int_connect_socket_add_02_good();
    printLine("Finished good()");
#endif /* OMITGOOD */
#ifndef OMITBAD
    printLine("Calling bad()...");
    CWE190_Integer_Overflow__int_connect_socket_add_02_bad();
    printLine("Finished bad()");
#endif /* OMITBAD */
}
#endif

接下來懷疑是返回值的問題,又添加了返回類型,有查過引用的標準庫,最後運用的控制變量法,發現了問題

是宏定義出了錯誤

#ifdef INCLUDEMAIN

......

#endif

#ifdef 語句1
  語句2
  #endif
  這種語句的意思是如果存在宏定義了語句1則執行語句2,它的作用是可以用它區隔一些與特定頭文件、程序庫和其他文件版本有關的代碼。
這代碼是用於測試使用的,我看不出開發者的用意,就是驗證是否存在INCLUDEMAIN這個宏定義,我也查找過了,不存在這個定義,所以自然不執行語句2,也就找不到main函數入口了,刪除這條語句,走起~一切ok。

main(int argc, char * argv[])
{
    /* seed randomness */
    srand( (unsigned)time(NULL) );
#ifndef OMITGOOD
    printLine("Calling good()...");
    CWE190_Integer_Overflow__int_connect_socket_add_02_good();
    printLine("Finished good()");
#endif /* OMITGOOD */
#ifndef OMITBAD
    printLine("Calling bad()...");
    CWE190_Integer_Overflow__int_connect_socket_add_02_bad();
    printLine("Finished bad()");
#endif /* OMITBAD */
}

得到正常運行結果:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章