Specifying the assembly directory in the make file

I am relatively new to creating made files. I put together the main make file to create the library. I want to save all temporary .o files in the build directory and have a built-in executable file stored in the bin directory.

My directory structure looks like this:

root src/ include/ build/ bin/ Makefile 

and this my make file looks like this:

 SHLIB = pg_extlib SRC = src/file1.c \ src/file2.c OBJS = build/file1.o \ build/file2.o debug_build: gcc -g -fPIC -c $(SRC) -I`pg_config --includedir` -I`pg_config --includedir-server` -I/some/required/path/include -Iinclude gcc -shared -o bin/$(SHLIB).so $(OBJS) -lm -lpq -lmylib_core clean: rm -f $(SHLIB) $(OBJS) 

.O files are placed correctly in the build folder, but they also appear in the root folder (where the Makefile is located). How to fix it?

+4
source share
1 answer

I see how you get the object files ( .o ) in the root folder, but I don’t know how you get them in the assembly folder.

Let’s take it in stages. First we provide the object files with their own rules:

 # Note the use of "-o ..." build/file1.o: gcc -g -fPIC -c src/file1.c -I`pg_config --includedir` -I`pg_config --includedir-server` -I/some/required/path/include -Iinclude -o build/file1.o build/file2.o: gcc -g -fPIC -c src/file2.c -I`pg_config --includedir` -I`pg_config --includedir-server` -I/some/required/path/include -Iinclude -o build/file2.o debug_build: $(OBJS) gcc -shared -o bin/$(SHLIB).so $(OBJS) -lm -lpq -lmylib_core 

It is effective but rude. Object files now go to build/ , but there is a lot of redundancy, without dependency processing. Therefore, we have added the necessary prerequisites and suppose that you are using GNUMake (what you need), we can use Automatic variables (and I will shorten the -I line for reading only):

 build/file1.o: src/file1.c gcc -g -fPIC -c $< -I... -o $@ build/file2.o: src/file2.c gcc -g -fPIC -c $< -I... -o $@ debug_build: $(OBJS) gcc -shared -o bin/$(SHLIB).so $^ -lm -lpq -lmylib_core 

Note that the commands in the object rules now exactly match. Thus, we can combine these two rules in several ways. The easiest way:

 build/file1.o: src/file1.c build/file2.o: src/file2.c build/file1.o build/file2.o: gcc -g -fPIC -c $< -I... -o $@ 

Now one more or two small tricks, and we will be glad:

 build/file1.o: src/file1.c build/file2.o: src/file2.c build/file1.o build/file2.o: gcc -g -fPIC -c $< -I`pg_config --includedir` -I`pg_config --includedir-server` -I/some/required/path/include -Iinclude -o $@ debug_build: $(OBJS) gcc -shared -o bin/$(SHLIB).so $^ -lm -lpq -lmylib_core 

There are more complicated tricks, but this time it should be a lot.

+5
source

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


All Articles