hdt80 hdt80 - 2 months ago 15
C++ Question

Adding the -M flag to g++ causes a file format not recoginzed error

Adding the

-m
flag to g++ causes a
file format not recognized; treating as linker script
, which in turns causes a syntax error when linking.

I'm using this makefile:

# Compilers
CXX = g++
#CXX = clang++
CC = gcc

UNAME := $(shell uname)

# Directories used for input and output
SRCDIR = src/src
BUILDDIR = build
EXEDIR = bin
INCLUDEDIR = src/include
VERBOSE = 0

# Debug flags
ifeq ($(VERBOSE), 1)
CXX_FLAGS += -M
endif

# Enable all warnings but format and unused variables
CXX_FLAGS += -Wall -Wno-format -Wno-unused-variable -Wno-varargs -c -g -O0 -fbuiltin -fpermissive -std=c++14 -I include -I $(INCLUDEDIR)

OUTPUT_NAME = Test

# Where the sources are located
SRCS = $(wildcard $(SRCDIR)/*.cpp)
SRCS += $(wildcard $(SRCDIR)/*/*.cpp)

CSRCS = $(wildcard $(SRCDIR)/*.c)

# Where the compiled objects are located
OBJS = $(patsubst $(SRCDIR)/%.cpp, $(BUILDDIR)/%.o, $(SRCS))
COBJS = $(patsubst $(SRCDIR)/%.c, $(BUILDDIR)/%.o, $(CSRCS))

# Linking all the .o files and with the libs
build: $(OBJS) $(COBJS)
$(CXX) $^ $(LINKER_FLAGS) -o ./bin/$(OUTPUT_NAME)

# Compiling all the .cpp files into .o files
$(OBJS): $(BUILDDIR)/%.o : $(SRCDIR)/%.cpp
$(CXX) $(CXX_FLAGS) -o "$@" "$<"

$(COBJS): $(BUILDDIRT)/%.o : $(SRCDIR)/%.c
$(CC) $(CC_FLAGS) -o "$@" "$<"

# Running the created exe
.PHONY: run
run:
./$(EXEDIR)/$(OUTPUT_NAME)


In
src/src
I have two files,
test.cpp
and
foo.cpp
.

test.cpp
:

#include "foo.h"
#include "cstdio"

int main(int argc, char* argv[]) {
Foo f;
int b = f.bar(2);

printf("%d", b);
return 0;
}


foo.cpp
:

#include "foo.h"

Foo::Foo() {

}

Foo::~Foo() {

}

int Foo::bar(int c) {
return c + c;
}


The
.h
file for
foo.cpp
is in
src/include
:

foo.h
:

#ifndef _FOO_H
#define _FOO_H

class Foo {
public:
Foo();
~Foo();

int bar(int c);
};

#endif


Calling
make build
compiles the code fine, and I get the output I expect, 4. But by calling
make VERBOSE=1 build
(which turns on the -M flag for g++) I get

/usr/bin/ls:build/foo.o: file format not recognized; treating as linker script


usr/bin/ld:build/foo.o:1: syntax error


I'm confused as to why enabling the
-M
flag would cause this, because I thought that
-M
output the dependencies of the program. If someone could point me in the correct direction I would really appreciate it.

Answer

The -M flag makes the compiler output dependencies to wherever it would otherwise have put its object file, as Barry explained. It does not print the dependencies on screen. Additionally, it disables the compiler altogether -- only preprocessing is performed.

If you were using the -M option to generate dependency files as input for make, you could use the -MD option to make the compiler output dependencies to a .d file alongside the object file. (In your case, for instance, that would make it generate a foo.d file along with the object file foo.o.)

Since it sounds like you just want to print the dependencies to the screen, though, you can use the -MF option to redirect the dependency output, e.g.

-M -MF /dev/stdout
Comments