Hello guys.. I have this makefile:

MyMatrix‬‬: MyMatrix.o
	gcc MyMatrix.o -o MyMatrix

MyMatrix.o: MyMatrix.c
	gcc -c MyMatrix.c

MyStringMain: MyString.o MyStringMain.o
	gcc  MyString.o MyStringMain.o -o MyString -o MyStringRun

MyStringMain.o: MyStringMain.c MyString.c MyString.h
	gcc -c MyStringMain.c 

MyString.o: MyString.c MyString.h
	gcc -c MyString.c


.PHONY:clean
clean: 
	rm -f *.o test.out

For some reason, only the MyMatrix file is created.. Any ideas why?

*p.s: all the files are good and have no errors.

Dani AI

Generated

Short answer: make builds the first target it finds by default, so your Makefile only produced the first executable. ’s quick change forced the other target to be built too (which is why reported it worked), but a clearer and more maintainable approach is to define an explicit default target (commonly named all) that lists every executable you want built.

Here’s a compact pattern-based example that demonstrates the idea without tying binaries together:

CC=gcc
CFLAGS=-Wall -g

all: MyMatrix MyStringRun

MyMatrix: MyMatrix.o
    $(CC) $(CFLAGS) $^ -o $@

MyStringRun: MyStringMain.o MyString.o
    $(CC) $(CFLAGS) $^ -o $@

%.o: %.c
    $(CC) $(CFLAGS) -c $< -o $@

.PHONY: all clean
clean:
    rm -f *.o MyMatrix MyStringRun

Practical tips and traps to watch for:

  • To see what make would run without executing, use make -n; for the default goal and variables use make -p; for verbose debugging try make -d. To build a single program, run make MyStringRun.
  • Don’t try to give gcc two -o options in one invocation expecting two executables — only the last -o takes effect. If you need two names, link twice or copy the resulting binary.
  • Watch for filename/target mismatches and hidden characters (copy/paste or right-to-left marks can sneak into names). Reveal them with ls -b, cat -v, or od -c and ensure exact, case-sensitive matches.

This keeps targets independent, makes the default build obvious, and avoids surprising behavior when adding more programs.

Recommended Answers

All 2 Replies

Try this one

MyMatrix‬‬: MyMatrix.o MyStringMain
	gcc MyMatrix.o -o MyMatrix

MyMatrix.o: MyMatrix.c
	gcc -c MyMatrix.c

MyStringMain: MyString.o MyStringMain.o
	gcc  MyString.o MyStringMain.o -o MyString -o MyStringRun

MyStringMain.o: MyStringMain.c MyString.c MyString.h
	gcc -c MyStringMain.c 

MyString.o: MyString.c MyString.h
	gcc -c MyString.c


.PHONY:clean
clean: 
	rm -f *.o test.out

Thanks a lot gerard. That worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.