Makefile with directory for object files

Dean

I have the following structure for a project and I am just starting to introduce a Makefile to build the software:

├── Makefile
├── README.md
├── cg
│   └── cg.c
└── utilities
   ├── utilities.c
   └── utilities.h

I am trying to put object files in a directory called obj yet I can't seem to get it working.

My makefile looks like:

CC=mpicc
CFLAGS=-O3 -std=c99
LIBS=
MKDIR_P = mkdir -p

make_build_dir:
    @mkdir -p obj/

utilities.o: utilities/utilities.c
    $(CC) $(CFLAGS) -o ./obj/$@ -c $<

cg.o: cg/cg.c
    $(CC) $(CFLAGS) -o ./obj/$@ -c $<

.PHONY: make_build_dir

cg.exe: make_build_dir utilities.o cg.o 
    $(CC) $(CFLAGS) -o $@ $<

clean:
    rm -fr obj
    rm cg.exe

Yet this generates the following error:

a@a:b/b ‹master*›$ make cg.exe
mpicc -O3 -std=c99 -o ./obj/utilities.o -c utilities/utilities.c
mpicc -O3 -std=c99 -o ./obj/cg.o -c cg/cg.c
cg/cg.c:133:3: warning: implicit declaration of function 'decompose' is invalid in C99
      [-Wimplicit-function-declaration]
  decompose(num_chunks, chunks_per_rank,me, &settings); 
  ^
1 warning generated.
mpicc -O3 -std=c99 -o cg.exe make_build_dir
clang: error: no such file or directory: 'make_build_dir'
make: *** [cg.exe] Error 1

How can I get it to generate the object files in the obj directory and then an executable in the top-level directory?

lubgr

This linking part of the makefile

cg.exe: make_build_dir utilities.o cg.o 
    $(CC) $(CFLAGS) -o $@ $<

has two issues. First, $< refers to the first prerequesite of the target cg.exe, and that is make_build_dir. Declaring it as .PHONY doesn't help here, it's simply passed to $(CC). Second, utilities.o cg.o both don't exist at this location. You can change the rule to

cg.exe: obj/utilities.o obj/cg.o 
    $(CC) $(CFLAGS) -o $@ $^

Note the automatic variable $^ which refers to all prerequisites. Additionally, the object file targets should be

obj/cg.o: cg/cg.c
    $(CC) $(CFLAGS) -o $@ -c $<

(identical for utilities.o).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Makefile not allowing me to specify directory for object files

From Dev

Create object files in specific directory through Makefile

From Dev

Makefile - move object files

From Dev

How to write a makefile where the compiled object files are in a different directory with a different name?

From Dev

How to compile sources in different directories into object files all in a single directory (Makefile)?

From Dev

Makefile should search for .o files in another directory

From Dev

Makefile: Error referencing source files in "src" directory

From Dev

Automatic Ordering of Object Files "*.o" in a Fortran Makefile

From Dev

Makefile doesn't clean object files

From Dev

Makefile rules vs source and object files

From Dev

Makefile process all files in one directory, output to another.

From Dev

How to compile multiple separate C files in a directory using a Makefile?

From Dev

Removing multiple files from directory, as specified by variable in Makefile

From Dev

Makefile process all files in one directory, output to another.

From Dev

Struggling to get a Makefile running with separate header and source files directory

From Dev

Makefile to render all targets of all .Rmd files in directory

From Dev

Store object files in separate directory using make

From Dev

Automake: Why is my Makefile picking files from the source directory instead of the build directory?

From Dev

Create multple object files at once without using a makefile?

From Dev

how to make Makefile to compile the multiple c files and h files in one directory

From Dev

MAKE: compiling all files once AND generating object files into different directory

From Dev

Include files in Makefile at the start of a makefile or at the end of the makefile

From Dev

Makefile path directory using ~/

From Dev

Current working directory of makefile

From Dev

Invoking the Makefile of parent directory

From Dev

Makefile directory creation for subdirectories

From Dev

Directory independent target in Makefile

From Dev

Makefile remaking directory target

From Dev

Directory independent target in Makefile

Related Related

  1. 1

    Makefile not allowing me to specify directory for object files

  2. 2

    Create object files in specific directory through Makefile

  3. 3

    Makefile - move object files

  4. 4

    How to write a makefile where the compiled object files are in a different directory with a different name?

  5. 5

    How to compile sources in different directories into object files all in a single directory (Makefile)?

  6. 6

    Makefile should search for .o files in another directory

  7. 7

    Makefile: Error referencing source files in "src" directory

  8. 8

    Automatic Ordering of Object Files "*.o" in a Fortran Makefile

  9. 9

    Makefile doesn't clean object files

  10. 10

    Makefile rules vs source and object files

  11. 11

    Makefile process all files in one directory, output to another.

  12. 12

    How to compile multiple separate C files in a directory using a Makefile?

  13. 13

    Removing multiple files from directory, as specified by variable in Makefile

  14. 14

    Makefile process all files in one directory, output to another.

  15. 15

    Struggling to get a Makefile running with separate header and source files directory

  16. 16

    Makefile to render all targets of all .Rmd files in directory

  17. 17

    Store object files in separate directory using make

  18. 18

    Automake: Why is my Makefile picking files from the source directory instead of the build directory?

  19. 19

    Create multple object files at once without using a makefile?

  20. 20

    how to make Makefile to compile the multiple c files and h files in one directory

  21. 21

    MAKE: compiling all files once AND generating object files into different directory

  22. 22

    Include files in Makefile at the start of a makefile or at the end of the makefile

  23. 23

    Makefile path directory using ~/

  24. 24

    Current working directory of makefile

  25. 25

    Invoking the Makefile of parent directory

  26. 26

    Makefile directory creation for subdirectories

  27. 27

    Directory independent target in Makefile

  28. 28

    Makefile remaking directory target

  29. 29

    Directory independent target in Makefile

HotTag

Archive