c - 如何使c程序在Windows 7中可执行

标签 c gcc

对于一个简单的“hello world”应用程序,我使用以下命令使用 GCC 编译器创建一个 .exe 文件:

cpp hello.c > hello.i

(成功)

gcc -S hello.i

(成功)

as -o hello.o hello.s

(成功)

当最终使用以下命令链接文件以获取 .exe 时,出现错误:

C:\C_Experiments\test>ld -o test2.exe test2.o
test2.o:test2.c:(.text+  0  x  9): undefined reference to __main
test2.o:test2.c:(.text+0 x    15): undefined reference to printf
ld: test2.o: bad reloc address 0x0 in section `.pdata'

最佳答案

您需要链接包含启动函数和所有其他标准函数的运行库。

为什么不跳过预处理程序和汇编程序步骤,直接进入目标文件呢?并且在链接时还使用 gcc 会自动添加所需的额外库吗?或者对于简单的单源文件程序直接转到可执行文件?

要么

$ gcc hello.c -c -o hello.o
$ gcc hello.o -o hello

或者

$ gcc hello.c -o hello

关于c - 如何使c程序在Windows 7中可执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17264171/

相关文章:

linux - 如何将存档的所有对象包含在共享对象中?

c - 多个相同的 I2C 传感器与 vl53L0x API (ST Microelectronics)

c++ - 如何调用具有多个不同签名的函数(如 glibc 调用 main)?

c - 错误 : permission denied for language c

c++ - const BYTE * 和 const LPBYTE

c - 错误 LNK2019 : unsresolved external symbol

assembly - 在 GCC 内联汇编中影响内存操作数寻址模式的早期破坏者的不正确行为的具体示例?

c - 时间测量因微 Controller 而异

c - 理解问题 - 不使用分支时最多两个数字

c - 为什么这个移植函数适用于二叉搜索树?