Solved clang++: fatal error: no input files when using Makefile

eternal_noob

Aspiring Daemon

Reaction score: 713
Messages: 980

Hi,

i want to create a Makefile to simplify the compilation of my C++ programs.
It should recursively collect all source files in a parent src directory and automatically create dependency files.

I found an example here but if i try to use it, clang++ complains:
Code:
[xxx@xxx ~/development]$ make
mkdir -p ./build
clang++ -Wfatal-errors -Wall -Wextra -Wpedantic -Wconversion -Wshadow  -o ./build/mybin
clang++: fatal error: no input files
*** Error code 1

Stop.
make: stopped in /home/xxx/development

I understand that the Makefile fails to append the sources to the clang++ command.
Is this because i use shells/bash as my shell? How can i make the Makefile work?

Makefile
Code:
CXX = clang++
CXX_FLAGS = -Wfatal-errors -Wall -Wextra -Wpedantic -Wconversion -Wshadow

# Final binary
BIN = mybin
# Put all auto generated stuff to this build dir.
BUILD_DIR = ./build

# List of all .cpp source files.
CPP = $(wildcard src/*.cpp)

# All .o files go to build dir.
OBJ = $(CPP:%.cpp=$(BUILD_DIR)/%.o)
# Gcc/Clang will create these .d files containing dependencies.
DEP = $(OBJ:%.o=%.d)

# Default target named after the binary.
$(BIN) : $(BUILD_DIR)/$(BIN)

# Actual target of the binary - depends on all .o files.
$(BUILD_DIR)/$(BIN) : $(OBJ)
    # Create build directories - same structure as sources.
    mkdir -p $(@D)
    # Just link all the object files.
    $(CXX) $(CXX_FLAGS) $^ -o $@

# Include all .d files
-include $(DEP)

# Build target for every single object file.
# The potential dependency on header files is covered
# by calling `-include $(DEP)`.
$(BUILD_DIR)/%.o : %.cpp
    mkdir -p $(@D)
    # The -MMD flags additionaly creates a .d file with
    # the same name as the .o file.
    $(CXX) $(CXX_FLAGS) -MMD -c $< -o $@

.PHONY : clean
clean :
    # This should remove all generated files.
    -rm $(BUILD_DIR)/$(BIN) $(OBJ) $(DEP)

src/main.cpp
Code:
#include <iostream>

int main(int argc, char* argv[])
{
    std::cout << "HElloe" << std::endl;
    return 0;
}

Thanks.
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 12,696
Messages: 39,295

To be honest I don't know the exact differences between the two either. But when people talk about a Makefile in relation to GCC and/or Linux it's almost always gmake(1).
 
OP
eternal_noob

eternal_noob

Aspiring Daemon

Reaction score: 713
Messages: 980

Since i didn't find proper documentation about BSD make, i am now generating the Makefile via cmake.
A bit overkill, but it's always fun to learn something new. :)
 
Top