Missing separator in Makefile?
Asked Answered
U

8

43

The following Makefile is not working and I am not sure what's going on.

CC = gcc
CFLAGS = -Wall -g

demo:
    ${CC} ${CFLAGS} demo.c -o demo
lib:
    ${CC} ${CFLAGS} lib.c -o lib
clean:
    rm -f lib demo

Demo has the main function and lib has a set of methods used in demo.

I added the -c flag to lib. However when I run make, I get:

Makefile:5: *** missing separator.  Stop.
Ustulation answered 6/3, 2012 at 8:59 Comment(3)
What do you mean by "not working"? What errors are you getting? What is happening? What is not happening?Margemargeaux
@Kobi, that's almost certainly because your command lines don't start with a tab character. See my answer for detail.Basrhin
Does this answer your question? Make error: missing separatorCalicle
B
88

Given your update with the error, check what you have on the line before those ${CC} commands. Many make programs require a real tab character before the commands and editors that put in eight spaces (for example) will break them. That's more often than not the cause of the "Missing separator" errors.

You can see that with the following transcript. In the file, there are four spaces before the $(xyzzy):

xyzzy=echo
all:
    $(xyzzy) hello

So, when I make it, I get the same error as you:

pax> make
makefile:3: *** missing separator.  Stop.

But, when I edit it and turn those four spaces into a tab, it works fine:

pax> make
echo hello
hello

You also have a problem with the way you're trying to combine the source files together.

Without a -c flag to gcc, it will try to create a separate executable from each of those commands, almost certainly leading to linker errors. You're going to need something like (simple):

CC = gcc
CFLAGS = -Wall -g

# Just compile/link all files in one hit.
demo: demo.c lib.c
   ${CC} ${CFLAGS} -o demo demo.c lib.c

clean:
    rm -f demo

or (slightly more complex):

CC = gcc
CFLAGS1 = -Wall -g -c
CFLAGS2 = -g

# Link the two object files together.

demo: demo.o lib.o
   ${CC} ${CFLAGS2} -o demo demo.o lib.o

# Compile each source file to an object.

demo.o: demo.c
   ${CC} ${CFLAGS1} -o demo.o demo.c

lib.o: lib.c
   ${CC} ${CFLAGS1} -o lib.o lib.c

clean:
    rm -f demo

The problem with the first solution is that it unnecessarily compiles both programs even when only one is out of date. The second solution is a little more intelligent.

Basrhin answered 6/3, 2012 at 9:4 Comment(4)
I've added this to .editorconfig: [Makefile] indent_style = tab indent_size = 4Agapanthus
@DUzun, assuming that the Makefile in your comment is the file name, you may want to consider also doing it for makefile and *.mk as well (along with any other make file variants you want to use).Basrhin
A tab was needed in my case.Billi
I've copied a make file from internet and replacing the four spaces with tabs did the work for me. I'm using a mac and the code I copied was from a GNU system tutorial.Vania
R
13

Do you miss some "tab" (\t) before ${CC} by any chance?

Often my editor will replace tabs by spaces and I'd get this message.

CC = gcc
CFLAGS = -Wall -g

demo:
[TAB]${CC} ${CFLAGS} demo.c -o demo
lib:
[TAB]${CC} ${CFLAGS} lib.c -o lib
clean:
[TAB]rm -f lib demo
Recrimination answered 6/3, 2012 at 9:15 Comment(0)
C
8

I got the same problem solved by replacing the leading spaces with tab

Cession answered 14/11, 2021 at 16:7 Comment(0)
T
3

Same error. When I use vi here, special characters won't show.

Cause is simple: There should be a TAB not spaces in front of ${CC}.

Tiflis answered 10/12, 2018 at 23:32 Comment(1)
Use :set list to show whitespace.Reiterant
D
2

lib needs to be compiled as a library, not a program.

Try changing it to:

main:
    ${CC} ${CFLAGS} main.c lib.o -o main
lib:
    ${CC} ${CFLAGS} lib.c -c -o lib.o
Drachma answered 6/3, 2012 at 9:2 Comment(1)
Thanks, but the problem is still there. I want to link lib to main so it knows it can use the functions in lib.Ustulation
A
1

Indentation was my issue.

My IDE was using a TAB character instead of spaces. Opened the Makefile in vim, did a :retab reindented the line with 2 spaces and that was solved.

Ascidium answered 25/4, 2019 at 15:10 Comment(0)
H
0

Since main depends on lib:

demo: lib demo.c

and

${CC} ${CFLAGS} lib.c -c -o lib.o
Hardworking answered 6/3, 2012 at 9:5 Comment(0)
O
0

Makefile:3: *** missing separator. Stop. Replacing space with the tab worked for me

Onaonager answered 20/6 at 2:39 Comment(2)
This answer already exists. Please only post unique answers. Upvote existing answers instead.Reiterant
Answer already existsShit

© 2022 - 2024 — McMap. All rights reserved.