Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

77%
+5 −0
Q&A make: How to compile all files in a directory.

I am learning how to write makefile to compile a c program. I have a directory structure like this: . ├── include │   └── library.h ├── lib │   └── library.c ├── makefile └── obj My makef...

1 answer  ·  posted 1mo ago by Vanity Slug ❤️‭  ·  last activity 1mo ago by Vanity Slug ❤️‭

Question c make
#7: Post edited by user avatar Vanity Slug ❤️‭ · 2024-10-11T11:39:12Z (about 1 month ago)
  • []()I am learning how to write makefile to compile a c program. I have a directory structure like this:
  • ```
  • .
  • ├── include
  • │   └── library.h
  • ├── lib
  • │   └── library.c
  • ├── makefile
  • └── obj
  • ```
  • My makefile has this content:
  • ```make
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • $(DIR_OBJ)/library.o: $(DIR_LIB)/library.c $(DIR_INC)/library.h
  • gcc -c -o $@ $<
  • ```
  • And it works: `library.o` gets generated inside `obj` folder.
  • Now I want to make it generic so that ALL files inside `lib` folder get compiled to object files into `obj` folder, so I replace `library` with `%` (because based on my limited/wrong understanding that's how it *should* work), like so:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • $(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • but I get this error:
  • ```
  • make: *** No targets. Stop.
  • ```
  • I find this Q/A: https://stackoverflow.com/questions/33530341/no-targets-stop. Based on that I add `all:` target, so my makefile looks like this now:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • all: $(DIR_OBJ)/%.o
  • $(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • And I get this error:
  • ```
  • make: *** No rule to make target 'obj/%.o', needed by 'all'. Stop.
  • ```
  • Why am I getting these errors and how do I fix them? My goal is to compile all `*.c` files inside `lib` directory into `*.o` object files into `obj` directory.
  • ---
  • **Update 1** (after receiving an answer)
  • I also tried making use of wildcard but was not successful with it. (I did not mention it earlier because I thought this was not the way to go). For the record, here is the latest makefile I had:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • FILES_LIB=$(wildcard $(DIR_LIB)/*.c)
  • FILES_OBJ=$(patsubst $(DIR_LIB)/%.c,$(DIR_OBJ)/%.o,$(FILES_LIB))
  • all: $(FILES_OBJ)
  • $(DIR_OBJ)/%.o: $(FILES_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • []()I am learning how to write makefile to compile a c program. I have a directory structure like this:
  • ```
  • .
  • ├── include
  • │   └── library.h
  • ├── lib
  • │   └── library.c
  • ├── makefile
  • └── obj
  • ```
  • My makefile has this content:
  • ```make
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • $(DIR_OBJ)/library.o: $(DIR_LIB)/library.c $(DIR_INC)/library.h
  • gcc -c -o $@ $<
  • ```
  • And it works: `library.o` gets generated inside `obj` folder.
  • Now I want to make it generic so that ALL files inside `lib` folder get compiled to object files into `obj` folder, so I replace `library` with `%` (because based on my limited/wrong understanding that's how it *should* work), like so:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • $(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • but I get this error:
  • ```
  • make: *** No targets. Stop.
  • ```
  • I find this Q/A: https://stackoverflow.com/questions/33530341/no-targets-stop. Based on that I add `all:` target, so my makefile looks like this now:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • all: $(DIR_OBJ)/%.o
  • $(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • And I get this error:
  • ```
  • make: *** No rule to make target 'obj/%.o', needed by 'all'. Stop.
  • ```
  • Why am I getting these errors and how do I fix them? My goal is to compile all `*.c` files inside `lib` directory into `*.o` object files into `obj` directory.
  • ---
  • **Update 1** (after receiving an answer from mr Tsjolder‭ )
  • I also tried making use of wildcard but was not successful with it. (I did not mention it earlier because I thought this was not the way to go). For the record, here is the latest makefile I had:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • FILES_LIB=$(wildcard $(DIR_LIB)/*.c)
  • FILES_OBJ=$(patsubst $(DIR_LIB)/%.c,$(DIR_OBJ)/%.o,$(FILES_LIB))
  • all: $(FILES_OBJ)
  • $(DIR_OBJ)/%.o: $(FILES_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
#6: Post edited by user avatar Vanity Slug ❤️‭ · 2024-10-11T11:38:40Z (about 1 month ago)
  • []()I am learning how to write makefile to compile a c program. I have a directory structure like this:
  • ```
  • .
  • ├── include
  • │   └── library.h
  • ├── lib
  • │   └── library.c
  • ├── makefile
  • └── obj
  • ```
  • My makefile has this content:
  • ```make
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • $(DIR_OBJ)/library.o: $(DIR_LIB)/library.c $(DIR_INC)/library.h
  • gcc -c -o $@ $<
  • ```
  • And it works: `library.o` gets generated inside `obj` folder.
  • Now I want to make it generic so that ALL files inside `lib` folder get compiled to object files into `obj` folder, so I replace `library` with `%` (because based on my limited/wrong understanding that's how it *should* work), like so:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • $(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • but I get this error:
  • ```
  • make: *** No targets. Stop.
  • ```
  • I find this Q/A: https://stackoverflow.com/questions/33530341/no-targets-stop. Based on that I add `all:` target, so my makefile looks like this now:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • all: $(DIR_OBJ)/%.o
  • $(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • And I get this error:
  • ```
  • make: *** No rule to make target 'obj/%.o', needed by 'all'. Stop.
  • ```
  • Why am I getting these errors and how do I fix them? My goal is to compile all `*.c` files inside `lib` directory into `*.o` object files into `obj` directory.
  • []()I am learning how to write makefile to compile a c program. I have a directory structure like this:
  • ```
  • .
  • ├── include
  • │   └── library.h
  • ├── lib
  • │   └── library.c
  • ├── makefile
  • └── obj
  • ```
  • My makefile has this content:
  • ```make
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • $(DIR_OBJ)/library.o: $(DIR_LIB)/library.c $(DIR_INC)/library.h
  • gcc -c -o $@ $<
  • ```
  • And it works: `library.o` gets generated inside `obj` folder.
  • Now I want to make it generic so that ALL files inside `lib` folder get compiled to object files into `obj` folder, so I replace `library` with `%` (because based on my limited/wrong understanding that's how it *should* work), like so:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • $(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • but I get this error:
  • ```
  • make: *** No targets. Stop.
  • ```
  • I find this Q/A: https://stackoverflow.com/questions/33530341/no-targets-stop. Based on that I add `all:` target, so my makefile looks like this now:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • all: $(DIR_OBJ)/%.o
  • $(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • And I get this error:
  • ```
  • make: *** No rule to make target 'obj/%.o', needed by 'all'. Stop.
  • ```
  • Why am I getting these errors and how do I fix them? My goal is to compile all `*.c` files inside `lib` directory into `*.o` object files into `obj` directory.
  • ---
  • **Update 1** (after receiving an answer)
  • I also tried making use of wildcard but was not successful with it. (I did not mention it earlier because I thought this was not the way to go). For the record, here is the latest makefile I had:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • FILES_LIB=$(wildcard $(DIR_LIB)/*.c)
  • FILES_OBJ=$(patsubst $(DIR_LIB)/%.c,$(DIR_OBJ)/%.o,$(FILES_LIB))
  • all: $(FILES_OBJ)
  • $(DIR_OBJ)/%.o: $(FILES_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
#5: Post edited by user avatar Vanity Slug ❤️‭ · 2024-10-10T16:09:19Z (about 1 month ago)
  • How to compile all files in a directory.
  • make: How to compile all files in a directory.
#4: Post edited by user avatar Vanity Slug ❤️‭ · 2024-10-10T15:34:12Z (about 1 month ago)
  • []()I am learning how to write makefile to compile a c program. I have a directory structure like this:
  • ```
  • .
  • ├── include
  • │   └── library.h
  • ├── lib
  • │   └── library.c
  • ├── makefile
  • └── obj
  • ```
  • My makefile has this content:
  • ```make
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • $(DIR_OBJ)/library.o: $(DIR_LIB)/library.c $(DIR_INC)/library.h
  • gcc -c -o $@ $<
  • ```
  • And it works: `library.o` gets generated inside `obj` folder.
  • Now I want to make it generic so that ALL files inside `lib` folder get compiled to object files into `obj` folder, so I replace `library` with `%` (because based on my limited/wrong understanding that's how it *should* work), like so:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • $(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • but I get this error:
  • ```
  • make: *** No targets. Stop.
  • ```
  • I find this Q/A: https://stackoverflow.com/questions/33530341/no-targets-stop. Based on that I add `all:` target, so my makefile looks like this now:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • all: $(DIR_OBJ)/%.o
  • $(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • And I get this error:
  • ```
  • make: *** No rule to make target 'obj/%.o', needed by 'all'. Stop.
  • ```
  • Why am I getting this errors and how do I fix them? My goal is to compile all `*.c` files inside `lib` directory into `*.o` object files into `obj` directory.
  • []()I am learning how to write makefile to compile a c program. I have a directory structure like this:
  • ```
  • .
  • ├── include
  • │   └── library.h
  • ├── lib
  • │   └── library.c
  • ├── makefile
  • └── obj
  • ```
  • My makefile has this content:
  • ```make
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • $(DIR_OBJ)/library.o: $(DIR_LIB)/library.c $(DIR_INC)/library.h
  • gcc -c -o $@ $<
  • ```
  • And it works: `library.o` gets generated inside `obj` folder.
  • Now I want to make it generic so that ALL files inside `lib` folder get compiled to object files into `obj` folder, so I replace `library` with `%` (because based on my limited/wrong understanding that's how it *should* work), like so:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • $(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • but I get this error:
  • ```
  • make: *** No targets. Stop.
  • ```
  • I find this Q/A: https://stackoverflow.com/questions/33530341/no-targets-stop. Based on that I add `all:` target, so my makefile looks like this now:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • all: $(DIR_OBJ)/%.o
  • $(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • And I get this error:
  • ```
  • make: *** No rule to make target 'obj/%.o', needed by 'all'. Stop.
  • ```
  • Why am I getting these errors and how do I fix them? My goal is to compile all `*.c` files inside `lib` directory into `*.o` object files into `obj` directory.
#3: Post edited by user avatar Vanity Slug ❤️‭ · 2024-10-10T15:32:53Z (about 1 month ago)
  • []()I am learning how to write makefile to compile a c program. I have a directory structure like this:
  • ```
  • .
  • ├── include
  • │   └── library.h
  • ├── lib
  • │   └── library.c
  • ├── makefile
  • └── obj
  • ```
  • My makefile has this content:
  • ```make
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • $(DIR_OBJ)/library.o: $(DIR_LIB)/library.c $(DIR_INC)/library.h
  • gcc -c -o $@ $<
  • ```
  • And it works: `library.o` gets generated inside `obj` folder.
  • Now I want to make it generic so that ALL files inside `lib` folder get compiled to object files into `obj` folder, so I replace `library` with `%`, like so:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • $(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • but I get this error:
  • ```
  • make: *** No targets. Stop.
  • ```
  • I find this Q/A: https://stackoverflow.com/questions/33530341/no-targets-stop. Based on that I add `all:` target, so my makefile looks like this now:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • all: $(DIR_OBJ)/%.o
  • $(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • And I get this error:
  • ```
  • make: *** No rule to make target 'obj/%.o', needed by 'all'. Stop.
  • ```
  • Why am I getting this errors and how do I fix them? My goal is to compile all `*.c` files inside `lib` directory into `*.o` object files into `obj` directory.
  • []()I am learning how to write makefile to compile a c program. I have a directory structure like this:
  • ```
  • .
  • ├── include
  • │   └── library.h
  • ├── lib
  • │   └── library.c
  • ├── makefile
  • └── obj
  • ```
  • My makefile has this content:
  • ```make
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • $(DIR_OBJ)/library.o: $(DIR_LIB)/library.c $(DIR_INC)/library.h
  • gcc -c -o $@ $<
  • ```
  • And it works: `library.o` gets generated inside `obj` folder.
  • Now I want to make it generic so that ALL files inside `lib` folder get compiled to object files into `obj` folder, so I replace `library` with `%` (because based on my limited/wrong understanding that's how it *should* work), like so:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • $(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • but I get this error:
  • ```
  • make: *** No targets. Stop.
  • ```
  • I find this Q/A: https://stackoverflow.com/questions/33530341/no-targets-stop. Based on that I add `all:` target, so my makefile looks like this now:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • all: $(DIR_OBJ)/%.o
  • $(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • And I get this error:
  • ```
  • make: *** No rule to make target 'obj/%.o', needed by 'all'. Stop.
  • ```
  • Why am I getting this errors and how do I fix them? My goal is to compile all `*.c` files inside `lib` directory into `*.o` object files into `obj` directory.
#2: Post edited by user avatar Vanity Slug ❤️‭ · 2024-10-10T15:30:23Z (about 1 month ago)
  • How compile all files in a directory.
  • How to compile all files in a directory.
  • I am learning how to write makefile to compile a c program. I have a directory structure like this:
  • ```
  • .
  • ├── include
  • │   └── library.h
  • ├── lib
  • │   └── library.c
  • ├── makefile
  • └── obj
  • ```
  • My makefile has this content:
  • ```make
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • $(DIR_OBJ)/library.o: $(DIR_LIB)/library.c $(DIR_INC)/library.h
  • gcc -c -o $@ $<
  • ```
  • And it works: `library.o` gets generated inside `obj` folder.
  • Now I want to make it generic so that ALL files inside `lib` folder get compiled to object files into `obj` folder, so I replace `library` with `%`, like so:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • $(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • but I get this error:
  • ```
  • make: *** No targets. Stop.
  • ```
  • I find this Q/A: https://stackoverflow.com/questions/33530341/no-targets-stop. Based on that I add `all:` target, so my makefile looks like this now:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • all: $(DIR_OBJ)/%.o
  • $(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • And I get this error:
  • ```
  • make: *** No rule to make target 'obj/%.o', needed by 'all'. Stop.
  • ```
  • Why am I getting this errors and how do I fix them? My goal is to compile all `*.c` files inside `lib` directory into `*.o` object files into `obj` directory.
  • []()I am learning how to write makefile to compile a c program. I have a directory structure like this:
  • ```
  • .
  • ├── include
  • │   └── library.h
  • ├── lib
  • │   └── library.c
  • ├── makefile
  • └── obj
  • ```
  • My makefile has this content:
  • ```make
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • $(DIR_OBJ)/library.o: $(DIR_LIB)/library.c $(DIR_INC)/library.h
  • gcc -c -o $@ $<
  • ```
  • And it works: `library.o` gets generated inside `obj` folder.
  • Now I want to make it generic so that ALL files inside `lib` folder get compiled to object files into `obj` folder, so I replace `library` with `%`, like so:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • $(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • but I get this error:
  • ```
  • make: *** No targets. Stop.
  • ```
  • I find this Q/A: https://stackoverflow.com/questions/33530341/no-targets-stop. Based on that I add `all:` target, so my makefile looks like this now:
  • ```
  • DIR_INC=include
  • DIR_LIB=lib
  • DIR_OBJ=obj
  • all: $(DIR_OBJ)/%.o
  • $(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
  • gcc -c -o $@ $<
  • ```
  • And I get this error:
  • ```
  • make: *** No rule to make target 'obj/%.o', needed by 'all'. Stop.
  • ```
  • Why am I getting this errors and how do I fix them? My goal is to compile all `*.c` files inside `lib` directory into `*.o` object files into `obj` directory.
#1: Initial revision by user avatar Vanity Slug ❤️‭ · 2024-10-10T15:30:04Z (about 1 month ago)
How compile all files in a directory. 
I am learning how to write makefile to compile a c program. I have a directory structure like this:

```
.
├── include
│   └── library.h
├── lib
│   └── library.c
├── makefile
└── obj
```

My makefile has this content:

```make
DIR_INC=include
DIR_LIB=lib
DIR_OBJ=obj

$(DIR_OBJ)/library.o: $(DIR_LIB)/library.c $(DIR_INC)/library.h
	 gcc -c -o $@ $<
```

And it works: `library.o` gets generated inside `obj` folder.

Now I want to make it generic so that ALL files inside `lib` folder get compiled to object files into `obj` folder, so I replace `library` with `%`, like so:

```
DIR_INC=include
DIR_LIB=lib
DIR_OBJ=obj

$(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
	gcc -c -o $@ $<
```

but I get this error:

```
make: *** No targets.  Stop.
```

I find this Q/A: https://stackoverflow.com/questions/33530341/no-targets-stop. Based on that I add `all:` target, so my makefile looks like this now:

```
DIR_INC=include
DIR_LIB=lib
DIR_OBJ=obj

all: $(DIR_OBJ)/%.o

$(DIR_OBJ)/%.o: $(DIR_LIB)/%.c $(DIR_INC)/%.h
	gcc -c -o $@ $<
```

And I get this error:

```
make: *** No rule to make target 'obj/%.o', needed by 'all'.  Stop.
```

Why am I getting this errors and how do I fix them? My goal is to compile all `*.c` files inside `lib` directory into `*.o` object files into `obj` directory.