2016-03-07 72 views
3

我試圖做一個PE(Portable Executable將與Windows XP +工作並沒有依賴性。編程不依賴

我發現本指南非常有趣http://www.catch22.net/tuts/reducing-executable-size

#define WIN32_LEAN_AND_MEAN 

#include <windows.h> 

extern "C" int __cdecl __purecall(void) 
{ 
    return 0; 
} 

extern "C" int __cdecl main(); 

extern "C" int __cdecl mainCRTStartup(void) 
{ 
    int mainret; 

    mainret = main(); 

    ExitProcess(mainret); 
} 

我編譯它

g++ -Wall -o2 -s -nodefaultlibs main.cpp -o test.exe 

而且它給了我一堆錯誤:http://i.stack.imgur.com/fIFoD.png

C:\Users\Main.cpp\Desktop\crt>g++ -Wall -o2 -s -nodefaultlibs -lkernel32 main.cpp -o test.exe 
C:\Users\Main.cpp\AppData\Local\Temp\cczgOKUx.o:main.cpp:(.text+0x1c): multiple definition of `mainCRTStartup' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x280): first defined here 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x5): undefined reference to `__dyn_tls_init_callback' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x39): undefined reference to `__cpu_features_init' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x3e): undefined reference to `_fpreset' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x4b): undefined reference to `_CRT_glob' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x73): undefined reference to `__getmainargs' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x78): undefined reference to `_CRT_fmode' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x82): undefined reference to `_imp___iob' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x87): undefined reference to `_fmode' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x96): undefined reference to `_setmode' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x9b): undefined reference to `_CRT_fmode' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0xaa): undefined reference to `_setmode' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0xaf): undefined reference to `_CRT_fmode' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0xbe): undefined reference to `_setmode' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0xc3): undefined reference to `__p__fmode' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0xc9): undefined reference to `_fmode' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0xd0): undefined reference to `_pei386_runtime_relocator' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0xd8): undefined reference to `__main' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0xdd): undefined reference to `__p__environ' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x100): undefined reference to `_cexit' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x13f): undefined reference to `signal' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x195): undefined reference to `signal' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x1d0): undefined reference to `signal' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x217): undefined reference to `signal' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x233): undefined reference to `signal' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x250): more undefined references to `signal' follow 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x266): undefined reference to `_fpreset' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x28c): undefined reference to `_imp____set_app_type' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x2ac): undefined reference to `_imp____set_app_type' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x2c1): undefined reference to `_imp__atexit' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o:crt1.c:(.text+0x2d1): undefined reference to `_imp___onexit' 
C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/bin/ld.exe: C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o: bad reloc address 0x20 in section `.eh_frame' 
collect2.exe: error: ld returned 1 exit status 

據我瞭解,我的mainCRTStartup正在干擾我的編譯器添加的另一個mainCRTStartup。

我在Windows上使用TDM-GCC。

在使用-nostdlib而不是-nodefaultlibs的建議後,我得到的錯誤要少得多。

那些我現在有是

C:\Users\Main.cpp\AppData\Local\Temp\ccMYa3VN.o:main.cpp:(.text+0x11): undefined reference to `__main' 
C:\Users\Main.cpp\AppData\Local\Temp\ccMYa3VN.o:main.cpp:(.text+0x31): undefined reference to `[email protected]' 
+2

catch22教程是針對Visual Studio的。對於'g ++',程序是不同的;這是有道理的,因爲你正在'竊取'使用過的編譯器的標準庫。 – MicroVirus

+0

看來你還需要使用'nostdlib'而不是'nodefaultlibs':http://stackoverflow.com/questions/10119520/g-static-initialization-and-nostdlib – MicroVirus

+0

隨着'-nostdlib'錯誤的大量清單消失! – Chris

回答

4

GCC使用一些內部的子程序,包含在libgcc.a,這是不能被排除在外。
https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Link-Options.html#Link-Options

一個由-nostdlib繞過了標準庫和-nodefaultlibs 是libgcc.a中,內部的子程序庫,GCC用來 克服特定機器的不足之處,或有特殊需要的 一些語言。 (有關 libgcc.a的更多討論,請參閱與GCC輸出的接口。)在大多數情況下,即使您希望 避免使用其他標準庫,也需要libgcc.a。換句話說,當你指定 -nostdlib或-nodefaultlibs時,你通常應該指定-lgcc。這確保您沒有未解決的對內部GCC 庫子例程的引用。

P.S.函數ExitProcessSetUnhandledExceptionFilter來自kernel32.lib,即您必須在鏈接器流中包含。您可以減少庫,但不能省略系統和操作系統庫。