Hello,

I am having a few problems when trying to execute the a Makefile that will hopefully create a static library that I can use for future use. Here is my code below:

#-----------------------------------------------------------------------------
#   Usage of make file
#-----------------------------------------------------------------------------
# Clean operation:
#   make -f MakeClient clean
#
# Make operation:
#   make -f MakeClient
#
#


#OBJ = $(SRC:.cpp=.o)
OBJ_DIR = ./obj

OUT_DIR= ./lib
OUT_FILE_NAME = libclient.a

# include directories

# C++ compiler flags (-g -O2 -Wall)
CCFLAGS := -std=c++0x

# compiler
CCC = g++ 


# Enumerating of every *.cpp as *.o and using that as dependency
$(OUT_FILE_NAME): $(patsubst %.cpp,$(OBJ_DIR)/%.o,$(wildcard *.cpp))
    $(CCC) -static $(LIB_DIR) $(LIBS) -o $(OUT_DIR)/$@ $^

#Compiling every *.cpp to *.o
$(OBJ_DIR)/%.o: %.cpp dircreation
    $(CCC) -c $(CCFLAGS) -o $@ $<

dircreation:
    @mkdir -p $(OUT_DIR)
    @mkdir -p $(OBJ_DIR)

.PHONY : clean
clean:
    rm -f $(OBJ_DIR)/*.o $(OUT_DIR)/$(OUT_FILE_NAME) Makefile.bak

And I get the following error(s):

g++  -c -std=c++0x -o obj/FFT.o FFT.cpp
g++  -c -std=c++0x -o obj/complex.o complex.cpp
g++  -static   -o ./lib/libclient.a obj/FFT.o obj/complex.o
ld: library not found for -lcrt0.o
collect2: error: ld returned 1 exit status
make: *** [libclient.a] Error 1

The problem seems to be associated with this line:

# Enumerating of every *.cpp as *.o and using that as dependency
$(OUT_FILE_NAME): $(patsubst %.cpp,$(OBJ_DIR)/%.o,$(wildcard *.cpp))
    $(CCC) -static $(LIB_DIR) $(LIBS) -o $(OUT_DIR)/$@ $^

But cannot figure what the problem is.

Hopefully someone can help,

Thanks :)

Dani AI

Generated

This is a macOS toolchain/Makefile issue rather than a C++ language problem. As noted, passing -static to the compiler on macOS makes the linker look for a static C runtime/startup object (crt0.o) that Apple does not ship, so the link fails. (stackoverflow.com)

Create a static archive with the archiver instead of invoking the system linker with -static. On Unix-like systems the typical sequence is to collect object files into an archive with ar and then index it with ranlib (or use libtool -static on macOS). Example replacement for the link step:

ar rcs ./lib/libclient.a obj/*.o
ranlib ./lib/libclient.a

This produces libclient.a as an archive of .o files rather than trying to make a statically linked executable. (flylib.com)

Practical Makefile guidance: keep the compile rule as -c (no -static), and change the target that currently runs the linker to run ar/ranlib (or libtool -static -o ...). If a truly statically linked executable on macOS is required, that is nontrivial and generally unsupported without a custom toolchain or non-Apple linker; for most projects producing a proper .a archive is the correct and portable approach. (manpagez.com)

Notes: ranlib may be optional on some toolchains (some ar implementations add the index automatically), but calling ranlib is harmless and keeps the archive compatible with the linker on macOS. (unix.com)

Are you using a MAC? Because a quick google search makes it pretty clear that this is a MAC OS X issue.

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.