【问题标题】:What is Eclipse CDT is doing with 'make' under the hoodEclipse CDT 在后台使用“make”做什么
【发布时间】:2015-07-16 18:29:43
【问题描述】:

我在 Windows 7 上并安装了 MinGW/gcc。我正在使用Eclipse CDT plugin 编译和构建我的第一个简单的 C 程序,并试图了解该插件在幕后所做的究竟是什么。

我创建了一个新的“Hello World!” C项目目录结构如下:

helloworld/
    src/
        helloworld.c

helloworld.c 在哪里:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    puts("Hello World!");
    return EXIT_SUCCESS;
}

所以我在调试模式下创建了一个运行配置(与“发布模式”相反,不是典型 Eclipse 用语中的“调试配置”!)并运行我的应用程序,它运行良好,打印“Hello World!”到 Eclipse 控制台。

现在我正在查看我的文件系统,文件/项目结构如下:

helloworld/
    src/
        helloworld.c
    Debug/
        src/
            helloworld.d
            helloworld.o
            subdir.mk
        helloworld.exe
        makefile
        objects.mk
        source.mk

假设在 Eclipse 中运行我的运行配置(因此在 Eclipse 中编译/构建/运行helloworld)在Debug 下创建了所有内容。此外,我假设helloworld.dhelloworld.o 是已编译的二进制文件,而helloworld.exe 是打包的可执行文件,包含这些二进制文件以及它们链接到的所有内容(stdiostdlib)。我还假设 makefile 是实际的 Make 文件(buildscript),并且 *.mk 文件是该 buildscript 的输入。所以,对于初学者,如果这些假设有任何错误,请先纠正我!

当我打开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

请注意:我不是在找人向我解释 Make 的工作原理,我可以为此进行 RTFM ;-)

我只是想了解在 Eclipse 之外从命令行编译、构建和运行 helloworld 需要什么。我需要哪些命令行调用来完成此任务,为什么?一旦我看到这一点,结合阅读 Make 文档,我应该能够填补空白并理解正在发生的一切。

【问题讨论】:

  • 从命令行,我会告诉 Eclipse 迷路并直接调用 Mingw。只需配置您的 Windows PATH 以便它在 Mingw 的 bin 目录中查找。

标签: c gcc windows-7 makefile eclipse-cdt


【解决方案1】:

*.o 文件是编译创建的目标文件。这些文件通常由如下命令构建:

    Gcc -ansi -Wall -pedantic -c helloworld.c -o helloworld.o

(抱歉,gcc 的大写字母,我的 iPad 坚持要纠正我的打字错误)

*.exe 是实际的可执行文件,它可能包含也可能不包含库函数。这取决于静态与动态链接。可执行文件通常由以下人员创建:

    Gcc helloworld.o -o helloworld.exe 

*.d 文件是依赖文件,由 gcc 构建,试图确定文件之间的依赖关系,通常使用以下命令构建

    MAKEDEPEND = gcc -M $(CPPFLAGS) -o $*.d $<

(取自 make 在线文档的规则)。

所以,回答你的最后一个问题,从命令行编译,如下命令:

    Foo gcc -ansi -WAll -pedantic helloworld.c -o helloworld.exe

应该为您解决问题。请注意,编译器的标志是我喜欢使用的最低限度,您可能会有一组不同的开关。

希望对您有所帮助, T

【讨论】:

    【解决方案2】:

    这在一定程度上取决于 Eclipse 在文件 source.mkobjects.mk 中生成的路径,但很可能您需要将 cd 放入 Debug 文件夹中。

    在其中,您可以运行make all 来编译项目。

    如果 Eclipse 生成绝对路径,您可以在任何地方使用make -f .../path/to/helloworld/Debug/makefile all

    【讨论】:

    • 我很确定 Eclipse 总是会创建绝对路径,即使您将路径添加到相对于它们在工作区中的位置的子文件夹也是如此。我知道这是因为我遇到了一个讨厌的Eclipse bug
    • 我添加了警告性评论,因为我没有安装 CDT。我的观察仅来自 Makefile。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2011-03-24
    • 2014-02-07
    相关资源
    最近更新 更多