Using Eclipse for Makefile projects

Source: Internet
Author: User
Tags version control system

Recently, on the MCU on Eclipse website, I saw an article written by Erich Styger on the use of makefile to create a project in Eclipse, and the article explains it very well, so no one translates it for your colleagues ' reference. Of course limited to the individual level, there are improper, please correct me. Original URL: https://mcuoneclipse.com/2017/07/22/tutorial-makefile-projects-with-eclipse/

The benefit of Ides like Eclipse is that it makes it easy to work with projects. Because it can not only generate files, but also can get and automatically manage makefile files. But sometimes this may not be what I want because I need greater flexibility and control, or I want to use the same production files for my continuous integration and automated test systems. In this case, you will need to write your own makefile file.

There's one thing that doesn't rule out anything else: This article describes how to use the makefiles in eclipse to be comfortable with the managed build system in eclipse, but with unlimited make files:

Profile

Eclipse comes with a built-in build system (Managed make): I can configure the compiler linker settings, and Eclipse will handle the rest. In this tutorial, I'll show you how to use Eclipse to manually create and use the crafted make files. The recommended make files and templates can be easily used to control the build system. The proposed method requires that each new source file that is added to the project need to extend the make file, instead, to extend the automatic generation of dependent files for all included header files.

The benefits of using make files are:

    • Easier to store and track in a version control system than eclipse. cproject Files and Settings
    • Complete control of build and production processes
    • As part of the build process, this is trivial compared to other steps, such as using a version control system or an automated test system.
    • You can use the same build (make) in the IDE and externally (for example, on an automated build system)
    • And it's fun to write make files.

The Make file method requires an understanding of the build process, so it is helpful to know what to use with manage make, so I can take it as a base or inspiration.

Pre-conditions

To use the make file in eclipse, I need the following:

    • Eclipse IDE. I'm using NXP Mcuxpresso IDE v10.0.2 (based on neon), but any other eclipse release can do the same.
    • Use the Make utility to install the GNU tool chain. Usually it comes with a bundled eclipse IDE, or you can use the DIY toolchain.
    • The compiler and linker settings for your target device. Use the options in the existing sample project.
    • Launch and linker files. Again, you can borrow the existing sample project.
    • Apply the source file. I recommend starting with the "empty" main function.

A good way to get the necessary files and options is to look at a sample project that typically uses "manged make".

?? I've put the source of this project on the GitHub on. In the "Links" section at the end of this article, check the links for GitHub projects.

from Eclipse managed by Make

Using a managed production system, Eclipse detects files that exist in the project (for example, *. c) and automatically generates make files:

Generator settings

This generates the make file:

Generate Make File

With the build tool integration, I have a GUI that can set the tool's options:

Manage Build tool settings

All of this information is added to the make file, and if I do a build, it calls the makes utility (or builder) to build it using all the necessary command lines. Check the console view output to see what it does

Console View Output

With this, I should have everything to do a make file project.

Create Make File Project

I created a new make file project in Eclipse, the menu file> new project:

New Project

As with many things in eclipse, there are several ways to do things.

If I have existing code, I can use the ' makefile Project with existing code '. Then I can give a name and the existing Code location (folder). It then creates the project within the code location folder and adds all the source files to the project:

Create a new make file project with existing code

The common approach is to generate a C or C + + project:

The new C + + project

Then give the project a name and select an empty make file project with the supported Toolchain:

Create an empty Makefile project

Then press Next, and then make the remaining dialog boxes based on the selected toolchain. This will create an empty project such as the following:

Empty Makefile Project

Application files

To edit something, add a new source file:

Add a new source file

Name it main.c or whatever you like:

New Main.c File

Then add a main () routine with some code, such as

static int i;

void Main (void) {

for (;;) {

i++;

}

/* Do not leave main () */

}

Makefile file

Next, use the context menu in the project folder "new" > "File":

New file

Create a file named "Makefile" in your project:

Create Make File

This adds an empty make file.

CMSIS , System files and startup

The next step depends heavily on the device you are using. I use the NXP frdm-kl25z board in this example. For the sake of structuring, I created the source directory. The special "Debug" folder will be used to place objects and executable files.

Expanded project structure

Make file Syntax

The next step is to implement the make file content. The general make file is as follows:

All:hello.axf

Clean:

RM MAIN.O HELLO.AXF

hello.axf:main.o

Gcc-g-O Hello main.o

MAIN.O:

Gcc-c-G main.c

Each entry in the make file has the following syntax:

<target>:< Dependencies list >

<tab> the command to execute

?? Please note that each command requires a <TAB> characters!

There are two of all cleaning known as special targets. ' Clean ' is used to delete all temporary files and ' All ' is used to build the application.

Makefile of the content

In makefile, I have defined variables for deleting files, compilers, and linker:

############################

# commands to delete files

rm:= RM-RF

############################

# compiler

cc:= ARM-NONE-EABI-GCC

############################

# linker

ll:= ARM-NONE-EABI-GCC

Then I define a macro for the final executable file:

############################

# Binary/Can be built

exe:= \

./debug/mymakeproject.axf

Next is a list of all the target files. Because the target file will be placed in the ' Debug ' folder, this is part of the name:

############################

# Target File list

objs:= \

./DEBUG/MAIN.O \

./debug/startup_mkl25z4.o \

./debug/system_mkl25z4.o

The next thing is really cool. If main.c contains header files Main.h, I will have to write a rule

MAIN.O:MAIN.C Main.h

$ (CC)-C Main.c-o MAIN.O

But it was very painful: I had to write rules for each file, and I needed to keep track of the header files. Instead, I let the compiler later create a dependent file with a. d extension (for dependency). The dependent file main.d looks like this:

DEBUG/MAIN.O debug/main.d:source/main.c Source/main.h

Source/main.h:

If it is not a "clean" target, I include the file as an additional rule file:

############################

# contains generated dependent files (only dirty targets)

Ifneq ($ (makecmdgoals), clean)

IFNEQ ($ (Strip $ (c_deps)))

-Contains $ (c_deps)

endif

endif

Here is the set of compiler options I can use later:

############################

# Compiler Options

cc_options:=-C-STD = gnu99-o0-g-Functional part-FDATA-SECTIONS-FNO-BUILTIN-MCPU = Cortex-m0plus-mthumb-dcpu_mkl25z128vlk4-d_ _use_cmsis-i "CMSIS"-i "source"

Similarly, the linker's options:

############################

# Linker Options

ll_options:=-nostdlib-xlinker-map = "Debug/mymakeproject.map"-xlinker--gc-sections-xlinker-print-memory-usage- MCPU = Cortex-m0plus-mthumb-t Linksfile.ld-o $ (EXE)

The next goal is to build the application:

############################

#main (All) target

all:$ (EXE)

@echo "***finished building***"

Follow the target to make a "clean":

############################

#clean Target

Clean

-$ (RM) $ (executables) $ (OBJS) $ (EXE)

-$ (RM)./debug/*.map

[Email protected] ‘ ‘

Here are the rules that link the application to all object files:

############################

# Rule to link the executable

$ (EXE): $ (OBJS) $ (USER_OBJS) linkerfile.ld

@echo ' Building target: [email protected] '

@echo ' Invoking:linker '

ARM-NONE-EABI-GCC $ (ll_options) $ (OBJS) $ (LIBS)

@echo ' finished building target: [email protected] '

@echo '

Next, I specify the rules for building the object file. I must specify a rule for each subfolder that you want to use:

############################

# Rule to build the files in the source folder

./DEBUG/%.O:./source/%.c

@echo ' Building file: $< '

$ (CC) $ (cc_options)-MMD-MP-MF "$ (@:%.o=%.d)"-MT "$ (@:%.o=%.o)"-MT "$ (@:%.o=%.d)"-O "[email protected]" $< "

@echo ' finished building: $< '

@echo '

############################

# Rule to build the files in the CMSIS folder

./DEBUG/%.O:./cmsis/%.c

@echo ' Building file: $< '

$ (CC) $ (cc_options)-MMD-MP-MF "$ (@:%.o=%.d)"-MT "$ (@:%.o=%.o)"-MT "$ (@:%.o=%.d)"-O "[email protected]" $< "

@echo ' finished building: $< '

@echo '

?? Options -MMD-MP-MF " $ ( @ :%. o =%. D) "

-MT " $ ( @ :%. o =%. o) " generates a dependency. d file for each compiled source file .

Production target

To build your project, you can use the usual "make/build" and "clean" menus/commands in eclipse. But there's a cool way: build Goals!

To do this, open the Build target view (menu windows> display View > Others):

Make Create target

In the view, select the item, and then click the New Target button:

New target

To create a "clean" target:

Clean target

This adds a build target group to the project. Use the context menu to add a new target for all:

Create a new target

In this way, I can double-click or use the context menu to execute the target in makefile:

Create a target in the Eclipse project browser

Review

Managed authoring and building in Eclipse is a great feature: it's easy to use, and probably the right thing for a 98% use case like the IDE. But if I want to use Eclipse's normal make file, it's not difficult. Using the proposed make file template, if you add a new source to the project, I must extend the file list. Otherwise everything will be taken care of. Writing a document requires a learning phase, but this can be a good input.

The next thing you can do is make it happy.

RELATED LINKS

    • Projects and files in this article on GitHub: Https://github.com/ErichStyger/mcuoneclipse/tree/master/Examples/MCUXpresso/FRDM-KL25Z/MyMakeProject
    • Making Files Tutorial: https://mcuoneclipse.com/2012/02/13/make-my-make-with-eclipse-and-mcu10/
    • GNU Make:https://www.gnu.org/software/make/

Welcome attention:

Use Eclipse for makefile projects

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.