pic11
pic11

Reputation: 14943

GNU make: generate list of source files

Is it normal to generate the list of source files (.c, .cpp etc, not headers) automatically?

How would you do it? I am thinking to use find and sed.

EDIT: The project is a library. All source files are inside the project directory. No unrelated source files are there. All object files are generated using the same compiler options. I am thinking to generate the list of all source files and then a dependency file for each source file following Tromey's way. It is a viable approach?

MORE EDIT: It is a fairly large C++ library. The project is being developed. Minimising recompilation is highly desired.

Thanks.

Upvotes: 4

Views: 6119

Answers (2)

Beta
Beta

Reputation: 99084

Normal? It is common, but not wise.

Many people use Make wildcards or find or something similar to generate a list of all the source files that exist in a certain directory tree, then feed them to the compiler and link the objects together. This is a brittle solution that will get you into trouble. If a conflict appears among the source files (e.g. two separate definitions of void foo()) the linker will complain and it may not be obvious how to fix the problem. You may find yourself with a forest of source files, many of them unnecessary to your project, slowing down your builds and causing conflicts. And if you want to make use of some of these sources (but not all) in another executable, you'll have to resort to symbolic links or some other kludgery.

A better approach is to specify in the makefile which objects are necessary to a given target, then let Make figure out which sources to use. This is what Make is good at. There is no reliable way to maintain the object lists automatically, you just have to do it by hand, but it's not that much work; if you're changing them often enough that this is a real chore, then you're doing something wrong.

EDIT:
If the project is a library as you describe, then yes, this is a viable method, and a pretty good one. And Tromey's method will work quite nicely to prevent unnecessary recompilation.

Upvotes: 3

Nicholas Riley
Nicholas Riley

Reputation: 44321

With GNU make you can use wildcards. See this question for an example.

Upvotes: 3

Related Questions