How to get cpp files from different directories compiled into one folder?

I have several C ++ files distributed in several folders.

a_library/ file1.cpp file2.cpp category1/ file3.cpp file4.cpp 

They are called to be uniquely named. I want to compile all these files in C ++ to separate Object files in the obj/ directory.

I have a list of all source files with a relative path and their corresponding destination names.

 a_library/file1.cpp a_library/file2.cpp a_library/category1/file3.cpp a_library/category1/file4.cpp obj/file1.obj obj/file2.obj obj/file3.obj obj/file4.obj 

How can I create a rule that converts a C ++ file from the first list to a file object from the second?

These attempts do not work:

 obj/%.obj: %:cpp # ... %.obj: %.cpp # ... .cpp.obj: # ... 

I would not write such rules:

 obj/%.obj: a_library/%.cpp # ... obj/%.obj: a_library/category1/%.cpp # ... 
+6
source share
5 answers

Try installing VPATH :

 VPATH = a_library:a_library/category1 obj/%.o: %.cpp $(CXX) -c $(CPPFLAGS) $(CFLAGS) $(CXXFLAGS) -o $@ $< 

And to add a complete list of files (I would recommend that you explicitly list the files, do not use the $(wildcard ...) function) and the application link:

 files := main.cpp $(wildcard a_library/*.cpp) a_library/category1/file.cpp obj/application: $(patsubst %.cpp,obj/%.o,$(notdir $(files))) $(CXX) $(CFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $+ 

$(wildcard) has an annoying tendency to collect something in directories, for example, one-time test files or temporary files (if they have a suitable name: ~file.cpp ).

+5
source

One solution that I can think of: just create them in place using a simple rule, and then do the “collection phase” by moving the “.o” files to one folder.

Create a collect_objs target that depends on your $ (OBJS), and then your main goal should depend on collect_objs.

You can bypass using the shell

 dirs := $(shell find ./ -type d) collect_objs: $(dirs) for d in $+; do \ mv *.o YourDestDir/*.o done 

Of course, this implies using the UnxUtils package (with "find" and "mv") or Cygwin, since you are on Windows.

Another option is to create targets for each of your .c / .cpp file explicitly using some kind of tool. Grab python, navigate to the source directories and write for each .c / .cpp file

 obj/file_name.o: gcc -c path/fo/file_name.c -o obj/file_name.o 
+1
source

Use cmake to create an assembly configuration for you.

Some time ago I created a simple github project example.

0
source

The standard way is to have a Makefile in each folder and invoke recursively with include

These were my first two hits on 10 ^ 100:

http://owen.sj.ca.us/~rk/howto/slides/make/slides/makerecurs.html

http://www.gnu.org/savannah-checkouts/gnu/make/manual/html_node/Recursion.html

0
source

It is not related to this issue, since it is not related to Make, although I would like to show how I collect my projects now, after 3 years. Craftr is a Python-based meta-assembly system that encourages indirect assemblies outside the tree (e.g. build in a working tree). Creating object files and building a static library is as simple as

 # craftr_module(my_project) from craftr import * from craftr.ext.platform import cxx, ar objects = cxx.compile( sources = path.platform('**/*.cpp'), ) lib = ar.staticlib( output = 'myproj', inputs = [objects], ) 

Running craftr -eb will result in the following build product structure

 Craftfile file1.c file2.c category1/ file3.c file4.c build/ my_project/ obj/ file1.o file2.o category1/ file3.o file4.o libmyproj.a 
0
source

Source: https://habr.com/ru/post/921383/


All Articles