m4l490n
m4l490n

Reputation: 1692

How to create the makefiles for a c project comprised of several folders with code?

Here is the deal. I have a folder structure with several building blocks of code and I can't figure out how to create the necessary makefiles to compile the project.

I tried to make the following but it didn't work:

I created the project with the wizard in order to let eclipse to create the makefiles automatically and it compiled correctly.

I observed that the makefiles automatically created by eclipse are created in the workspace folder but I want them in my project folders.

What I did as a test was basically to replicate all the .mk files and the main makefile in my folder structure exactly as eclipse did in the workspace and create a new project with the create empty makefile option from the wizard but but it doesn't compile.

I don't know what else should I do or configure to make it work.

Thanks for helping.

Upvotes: 0

Views: 233

Answers (1)

Sami
Sami

Reputation: 155

As stated in the comment there are many ways to organize a build system. One easy that I usually use is following:

  1. Have a single shared compile variables defining .mk file.

  2. Have a single shared make file that has rules defined to build object file for different types of code files, for example .c,.cpp etc. Also include in this Makefile the rules to build the target. You can have multiple flavours of this Makefile for different type (static libs, shared libs, executables) of targets.

  3. In each folder define a make file that populates the list of objects files and also defined the target name. Include the Makefile(2) in this makefile.

  4. In each folder call the Makefile in the subfolders. You can build the static libs for each subfolder and include these in the "main" executable Makefile as dependency.

If you don't want to write Makefile for every folder/subfolder as defined in step 3. You can define one Makefile that builds every code file in folder and call it recursively.

I hope it helps.

Upvotes: 1

Related Questions