-3

我正在尝试编写一个makefile并编译了main.c。然后我正在尝试创建 main.o,但我对如何创建感到困惑。我在 UNIX 中使用 vi 编辑器。我试过gcc -o main.c了,我收到一个致命错误,说没有输入文件。什么地方出了错?

4

2 回答 2

5

You can use gcc's -c option to compile a source file without linking. This will leave you with a .o file:

gcc -c main.c

You can then create an executable by linking that .o file with the standard libraries, and other .o or .c files if you like:

gcc -o myprogram main.o

The primary advantage of this is when you have multiple .c files. In that case you can save time by not recompiling them all when one of them changes.

于 2013-02-24T03:37:28.007 回答
0

如果您使用的是 Makefile,那么您可能包含太多内容。警告:以下内容将覆盖您的 Makefile。尝试:

echo 'all: main.o' > Makefile
make

甚至:

> Makefile  # truncate the Makefile.  That is, make it empty
make main.o

甚至:

rm Makefile
make main.o

别这么辛苦了。

于 2013-02-24T13:13:25.683 回答