I'm on Windows 7 and have MinGW/gcc installed. I'm using the Eclipse CDT plugin to compile and build my first simple C programs, and am trying to follow what exactly the plugin is doing under the hood.
我在Windows 7上安装了MinGW / gcc。我正在使用Eclipse CDT插件来编译和构建我的第一个简单的C程序,并且我正在尝试关注插件正在做什么。
I create a new "Hello World!" C project with the following directory structure:
我创建了一个新的“Hello World!”具有以下目录结构的C项目:
helloworld/
src/
helloworld.c
Where helloworld.c
is:
helloworld.c在哪里:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
puts("Hello World!");
return EXIT_SUCCESS;
}
So I created a Run Configuration in Debug Mode (as opposed to "Release Mode", not a "Debug Configuration" in typical Eclipse parlance!) and ran my app, and it works beautifully, printing "Hello World!" to the Eclipse console.
所以我在调试模式下创建了一个运行配置(与“发布模式”相反,而不是典型的Eclipse说法中的“调试配置”!)并运行我的应用程序,它运行得很漂亮,打印“Hello World!”到Eclipse控制台。
Now I'm looking on my file system and the file/project structure is like so:
现在我正在查看我的文件系统,文件/项目结构如下:
helloworld/
src/
helloworld.c
Debug/
src/
helloworld.d
helloworld.o
subdir.mk
helloworld.exe
makefile
objects.mk
source.mk
I assume that running my Run Configuration in Eclipse (hence compiling/building/running helloworld
inside Eclipse) created everything under Debug
. Furthermore I assume that helloworld.d
and helloworld.o
are compiled binaries, and that helloworld.exe
is the packaged executable containing those binaries and everything they'red linked to (stdio
and stdlib
). I also assume makefile
is the actual Make file (buildscript), and that the *.mk
files are somehow inputs to that buildscript. So, for starters, if any of those assumptions are wrong, please begin by correcting me!
我假设在Eclipse中运行我的运行配置(因此在Eclipse中编译/构建/运行helloworld)在Debug下创建了所有内容。此外,我假设helloworld.d和helloworld.o是编译的二进制文件,而helloworld.exe是打包的可执行文件,包含那些二进制文件以及它们链接到的所有内容(stdio和stdlib)。我还假设makefile是实际的Make文件(buildscript),并且* .mk文件以某种方式输入到该buildcript。所以,对于初学者来说,如果这些假设中的任何一个是错误的,请首先纠正我!
When I open makefile
I see this:
当我打开makefile时,我看到了这个:
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
-include ../makefile.init
RM := rm -rf
# All of the sources participating in the build are defined here
-include sources.mk
-include src/subdir.mk
-include subdir.mk
-include objects.mk
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
endif
-include ../makefile.defs
# Add inputs and outputs from these tool invocations to the build variables
# All Target
all: helloworld
# Tool invocations
helloworld: $(OBJS) $(USER_OBJS)
@echo 'Building target: $@'
@echo 'Invoking: Cross GCC Linker'
gcc -o "helloworld" $(OBJS) $(USER_OBJS) $(LIBS)
@echo 'Finished building target: $@'
@echo ' '
# Other Targets
clean:
-$(RM) $(EXECUTABLES)$(OBJS)$(C_DEPS) helloworld
-@echo ' '
.PHONY: all clean dependents
.SECONDARY:
-include ../makefile.targets
Please note: I am not looking for someone to explain to me how Make works, I can RTFM for that ;-)
请注意:我不是在找人向我解释Make如何运作,我可以RTFM ;-)
I am just trying to understand what it would take to compile, build and run helloworld
from the command-line, outside of Eclipse. What command line invocations would I need to accomplish this, and why? Once I see that, combined with perusing Make docs, I should be able to fill in the gaps and understand everything that is going on.
我只想了解在Eclipse之外从命令行编译,构建和运行helloworld需要什么。我需要执行哪些命令行调用,为什么?一旦我看到这一点,结合阅读Make docs,我应该能够填补空白并理解正在发生的一切。
2 个解决方案
#1
That depends a bit on the paths that Eclipse generates in the files source.mk
and objects.mk
but most likely you need to cd
into the Debug
folder.
这取决于Eclipse在文件source.mk和objects.mk中生成的路径,但很可能需要cd进入Debug文件夹。
Inside of that, you can then run make all
to compile the project.
在其中,您可以运行make all来编译项目。
If Eclipse generated absolute paths, you can use make -f .../path/to/helloworld/Debug/makefile all
from anywhere.
如果Eclipse生成了绝对路径,则可以从任何地方使用make -f ... / path / to / helloworld / Debug / makefile。
#2
The *.o files are the object file(s) created by compilation. these files are typically build by a command like:
* .o文件是由编译创建的目标文件。这些文件通常由以下命令构建:
Gcc -ansi -Wall -pedantic -c helloworld.c -o helloworld.o
(apologies foe capitalization of gcc, my iPad insists on correct my typing)
(对于gcc的资本化道歉,我的iPad坚持纠正我的打字)
The *.exe is the actual executable, which may or may not contain the library functions. This depends on static versus dynamic linking. The executable is created typically by:
* .exe是实际的可执行文件,可能包含也可能不包含库函数。这取决于静态与动态链接。可执行文件通常由以下内容创建
Gcc helloworld.o -o helloworld.exe
The *.d files are dependency files, built by gcc attempting to determine dependencies between files, typically built with the following command
* .d文件是依赖文件,由gcc构建,用于确定文件之间的依赖关系,通常使用以下命令构建
MAKEDEPEND = gcc -M $(CPPFLAGS) -o $*.d $<
(Rule taken from make online documentation).
(从制作在线文档中获取的规则)。
So,to answer your final question, to compile from the command line, a command like:
所以,为了回答你的最后一个问题,从命令行编译一个命令,如:
Foo gcc -ansi -WAll -pedantic helloworld.c -o helloworld.exe
Should do the trick for you. Note, the flags to the compiler are the minimum that I like to use, you will probably have a different set of switches.
应该为你做的伎俩。注意,编译器的标志是我喜欢使用的最小值,你可能会有一组不同的开关。
Hopes this help, T
希望这有帮助,T
#1
That depends a bit on the paths that Eclipse generates in the files source.mk
and objects.mk
but most likely you need to cd
into the Debug
folder.
这取决于Eclipse在文件source.mk和objects.mk中生成的路径,但很可能需要cd进入Debug文件夹。
Inside of that, you can then run make all
to compile the project.
在其中,您可以运行make all来编译项目。
If Eclipse generated absolute paths, you can use make -f .../path/to/helloworld/Debug/makefile all
from anywhere.
如果Eclipse生成了绝对路径,则可以从任何地方使用make -f ... / path / to / helloworld / Debug / makefile。
#2
The *.o files are the object file(s) created by compilation. these files are typically build by a command like:
* .o文件是由编译创建的目标文件。这些文件通常由以下命令构建:
Gcc -ansi -Wall -pedantic -c helloworld.c -o helloworld.o
(apologies foe capitalization of gcc, my iPad insists on correct my typing)
(对于gcc的资本化道歉,我的iPad坚持纠正我的打字)
The *.exe is the actual executable, which may or may not contain the library functions. This depends on static versus dynamic linking. The executable is created typically by:
* .exe是实际的可执行文件,可能包含也可能不包含库函数。这取决于静态与动态链接。可执行文件通常由以下内容创建
Gcc helloworld.o -o helloworld.exe
The *.d files are dependency files, built by gcc attempting to determine dependencies between files, typically built with the following command
* .d文件是依赖文件,由gcc构建,用于确定文件之间的依赖关系,通常使用以下命令构建
MAKEDEPEND = gcc -M $(CPPFLAGS) -o $*.d $<
(Rule taken from make online documentation).
(从制作在线文档中获取的规则)。
So,to answer your final question, to compile from the command line, a command like:
所以,为了回答你的最后一个问题,从命令行编译一个命令,如:
Foo gcc -ansi -WAll -pedantic helloworld.c -o helloworld.exe
Should do the trick for you. Note, the flags to the compiler are the minimum that I like to use, you will probably have a different set of switches.
应该为你做的伎俩。注意,编译器的标志是我喜欢使用的最小值,你可能会有一组不同的开关。
Hopes this help, T
希望这有帮助,T