Using GCC to generate makefile dependencies.

  • Thread starter TylerH
  • Start date
  • Tags
    Gcc
In summary, the conversation discusses using GCC to generate dependencies for makefiles and using the -M option. The speaker encountered an error when trying to insert the name of the .cpp into the shell command. They suggest looking into Autotools as a solution, but also mention that hard-coding may be a better option for simpler compilation environments. They also ask for more information about the operating system and libraries being used.
  • #1
TylerH
729
0
How do I use GCC to generate dependencies for makefiles? I tried using the -M option, like below, but I don't know how to insert the name of the .cpp into the shell command that calls GCC with the -M option.

Code:
%.cpp : $(shell gcc -M $<)

Instead of the desired effect, $< is replaced by nothing, causing GCC to generate an error and my attempt to generate dependencies to fail.
 
Technology news on Phys.org
  • #2
I had the same problem that you do now. For that I did some googling around and found a set of tools called Autotools. Here is a pretty good explanation of what you might want to do:

http://www.freesoftwaremagazine.com/books/autotools_a_guide_to_autoconf_automake_libtool

This requires some effort to setup correctly (depends on the complexity of your compilation environment), so if you have only a small number of libraries, hard-coding would be a better option.

What operating system are you using? What libraries are you trying to use?
 

1. What is GCC?

GCC (GNU Compiler Collection) is a compiler used to compile programming languages such as C, C++, and Fortran. It is open-source and widely used in many software development projects.

2. How does GCC generate makefile dependencies?

GCC has a built-in feature that allows it to generate dependencies automatically. When compiling a source file, GCC will scan the file for #include statements and generate a list of header files that the source file depends on. This list can then be used to create a makefile with the necessary dependencies.

3. Why is it important to generate makefile dependencies?

Makefile dependencies ensure that a program is built correctly and efficiently. By listing all the dependencies, the makefile can track which files need to be recompiled when a change is made, saving time and avoiding errors.

4. How do I use GCC to generate makefile dependencies?

To use GCC to generate makefile dependencies, you can add the -MMD flag to your compiler command. This will create a .d file with the list of dependencies. You can then include this file in your makefile to automatically track dependencies.

5. Can GCC generate makefile dependencies for multiple source files?

Yes, GCC can generate makefile dependencies for multiple source files. You can use the -MMD flag for each source file, and then include all the generated .d files in your makefile. Alternatively, you can use the -MD flag, which will generate a single .d file with dependencies for all the source files.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
2
Views
5K
  • Programming and Computer Science
Replies
1
Views
638
  • Programming and Computer Science
Replies
9
Views
8K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
13
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Programming and Computer Science
Replies
5
Views
9K
Back
Top