I'm trying to link my own static library into my main program. My include headers and libraries are in the same path. g++ is able to link the main headers just fine, but its unable to find my library (ipc.a). Please let me know what am I doing wrong.
Error I'm getting when I run make is :
# make
g++ -o esim esim.o -L /home/vint/HobbyProjects/esim/src/LIB/PLAT -lipc -Wall -g
/usr/bin/ld: cannot find -lipc
collect2: ld returned 1 exit status
Makefile is given below
INC_DIR = /home/vint/HobbyProjects/esim/src/LIB/PLAT
LIB_DIR = /home/vint/HobbyProjects/esim/src/LIB/PLAT
INCLUDES = -I $(INC_DIR)/
LIBS = -L$(LIB_DIR)/
LIBA = -lipc
CC = g++
DEBUG = -g
LFLAGS = -Wall $(DEBUG)
CFLAGS = -Wall -c
SOURCES = esim.cpp \
HEADERS = esim.h
OBJ = $(SOURCES:.cpp=.o)
EXE = esim
all: esim
$(EXE): $(OBJ)
$(CC) $(OBJ) $(INCLUDES) $(LIBA) -o $(EXE)
.cpp.o:
$(CC) $(CFLAGS) $(INCLUDES) $< -o $#
tar:
tar -czf $(EXE).tar.gz $(SOURCES) $(HEADERS) Makefile
clean:
rm -f $(OBJ)
rm -f $(EXE)
The problem is that you don't add -L/home/vint/HobbyProjects/esim/src/LIB/PLAT option when compiling by the makefile.
Change:
$(EXE): $(OBJ)
$(CC) $(OBJ) $(INCLUDES) $(LIBA) -o $(EXE)
Into:
$(EXE): $(OBJ)
$(CC) $(OBJ) $(INCLUDES) $(LIBA) $(LIBS) -o $(EXE)
Related
Ok I Have a problem making a MakeFile for C++ project in Windows. I checked around and found some tutorials and this is what I got:
IDIR =../include
CC = g++
CFLAGS=-I$(IDIR)
ODIR = obj
LDIR =../lib
LIBS = -lsfml-graphics -lsfml-audio -lsfml-window -lsfml-system
_DEPS = Button.hpp
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
_OBJ = Test.o Button.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
$(ODIR)/%.o: %.c $(DEPS)
$(CC) -c -o $# $< $(CFLAGS)
Test.exe: $(OBJ)
$(CC) -o $# $^ $(CFLAGS) $(LIBS)
.PHONY: clean
clean:
rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~
And this is the error message that pops up:
make: *** No rule to make target 'obj/Test.o', needed by 'Test.exe'. Stop.
EDIT:
Hi I'm still having trouble with my makefile. The point is that my project folder is a mess and I want to keep it neat.
My make file is having trouble working across multiple directories. Now all my cpp files, header files and object files are clumped in one folder and that works but since I'm adding more to my program it is becoming messy. I managed to make it work to have headers in one directory but objects are problematic hence I'm asking for help again.
This is my Makefile now:
IDIR = Headers
ODIR = obj
CC = g++
CPPFLAGS = -I$(IDIR) $(LIBS)
LIBS = -lm -lsfml-graphics -lsfml-audio -lsfml-window -lsfml-system
_DEPS = Button.hpp TextBox.hpp TextToWindow.hpp MessageBox.hpp CheckBox.hpp
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
_OBJ = Button.o TextBox.o TextToWindow.o MessageBox.o CheckBox.o Test.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
$(ODIR)/%.o: %.cpp $(DEPS)
$(CC) -c -o $# $< $(CPPFLAGS)
Test.exe: $(OBJ)
$(CC) -o $# $^ $(CPPFLAGS)
.PHONY: clean
clean:
del $(ODIR)*.o *.exe
And this is my working dir:
MainDir:
-Headers:
-hpp
-hpp
.
.
-cpp
-o
-cpp
.
.
It is working for headers but it cant make objects in that dir hence the error:
make: *** No rule to make target 'obj/Button.o', needed by 'Test.exe'. Stop.
Ok so I made it like this and it works, but I still can't make it work in multiple directories.
CC = g++
CPPFLAGS = -I $(LIBS)
LIBS = -lm -lsfml-graphics -lsfml-audio -lsfml-window -lsfml-system
DEPS = Button.hpp TextToWindow.hpp
OBJ = Test.o Button.o TextToWindow.o
%.o: %.cpp $(DEPS)
$(CC) -c -o $# $< $(CPPFLAGS)
Test.exe: $(OBJ)
$(CC) -o $# $^ $(CPPFLAGS)
.PHONY: clean
clean:
rm -f /*.o *~ core /*~
Oh, and the clean function doesn't work. If anyone can suggest a better way please do. Thanks!
INC_DIR = ./include
SRC_DIR = ./src
OBJ_DIR = ./obj
SRC_FILES = $(wildcard $(SRC_DIR)/*.cpp))
H_FILES = $(wildcard $(INC_DIR)/*.cpp)
OBJ_FILES=$(patsubst $(SRC_DIR)/%.cc,$(OBJ_DIR)/%.o,$(SRC_FILES)
TARGET = PT3
CC = g++
CFLAGS = - fPIC -c -Wall -Werror -pedantic -std=c++11 -Wno-c++11-extensions
CPPFLAGS = $(addprefix -I, $(INC_DIR))
clean:
rm -f $(OBJECTS) $(TARGET)
all: $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) $(CFLAGS) $(CPPFLAGS) -o $# $^
%.o: %.cc
$(CC) $(CFLAGS) $(CPPFLAGS) -o $# -c $<
g++ -shared -fPIC -o libtest.so $(OBJECTS)
main.o: main.cc
$(CC) $(CFLAGS) $(CPPFLAGS) -L/pt3/lib -o maintest main.cc -libtest
this is currently what I have and i know its not syntactically right or remotely working but Im getting stuck on creating the shared library so I dont even know what else wouldnt compile.**
INC_DIR = ./include
SRC_DIR = ./src
SRC_FILES = $(sort $(shell find $(SRC_DIR) -name '*.cc'))
OBJ_FILES = $(SRC_FILES:.cc=.o)
TARGET = PT3
CC = g++
CFLAGS = -fPIC -Wall -Werror -pedantic -std=c++11 -Wno-c++11-extensions
CPPFLAGS = $(addprefix -I, $(INC_DIR))
#clean:
# rm -f $(OBJECTS) $(TARGET)
all: $(TARGET)
$(TARGET): $(OBJ_FILES)
$(CC) $(CFLAGS) $(CPPFLAGS) -o $# $^
%.o: %.cc
$(CC) $(CFLAGS) $(CPPFLAGS) -o $# -c $<
libtest.so: $(OBJ_FILES)
$(CC) -shared -fPIC -o $# $^
maintest: main.o libtest.so
$(CC) $(CFLAGS) $(CPPFLAGS) -L. -o maintest main.o -libtest
this is what i rewrote the code to however Im getting a no input files error, but Im not sure if thats coming from a wrong read / failure to get into the required folders, or due to possibly missing a -o or -c?
Ive worked on the code some more following the suggestions and have come to this:
CXX = g++
CXXFLAGS := -Wall -Werror -pedantic -std=c++11 -Wno-c++11-extensions
SRC_FILES = $(wildcard $(SRC_DIR)/*.cpp)
OBJECTS=$(patsubst $(SRC_DIR)/%.cpp,$(OBJ_DIR)/%.o,$(SRC_FILES))
INC_DIR = include
SRC_DIR = src
OBJ_DIR = obj
TEST_DIR = tests
LIB_DIR = lib
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
$(CXX) $(CXXFLAGS) -fPIC -Iinclude -o $# -c $<
clean:
rm -f $(OBJECTS) $(TEST_DIR)/main.o
$(LIB_DIR)/libtest.so: $(OBJECTS)
#echo frank
$(CXX) -shared -fPIC -o $# $^
$(TEST_DIR)/main.o: $(TEST_DIR)/main.cc
$(CXX) $(CXXFLAGS) -Iinclude -o $# -c $<
maintest: $(TEST_DIR)/main.o $(LIB_DIR)/libtest.so
$(CXX) $(CXXFLAGS) -Llib -Iinclude -o $# $< -ltest
everything seems to compile fine however when running the maintest program it returns an error saying: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
thanks for the suggestions so far I feel like Im on the verge of actually getting the makefile working as intended
You've already written the recipe for the library (which builds it in the working directory -- we can change that later if you want):
g++ -shared -fPIC -o libtest.so $(OBJECTS)
The next step is to put it into a rule:
libtest.so: $(OBJECTS)
g++ -shared -fPIC -o libtest.so $(OBJECTS)
Then clean it up:
libtest.so: $(OBJECTS)
g++ -shared -fPIC -o $# $^
It looks as if you want the executable to be maintest, so let's write a rule for that:
maintest: main.o libtest.so
$(CC) $(CFLAGS) $(CPPFLAGS) -L. -o maintest main.o -ltest
Give that a try. We can make further adjustments once that much works.
EDIT: I see that there are a few other problems in your makefile. Your variables won't work as written. Do you name your source files "foo.cc" or "foo.cpp"?
EDIT: I can see that we'll have to do this in stages.
Step 1. Try this:
CXX = g++
CXXFLAGS := -Wall -Werror -pedantic -std=c++11 -Wno-c++11-extensions
INC_DIR = include
SRC_DIR = src
OBJ_DIR = obj
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
$(CXX) $(CXXFLAGS) -Iinclude -o $# -c $<
Try to build one or two object files with this, as in make obj/foo.o.
Step 2. Add a rule for main.o, and test it:
$(OBJ_DIR)/main.o: main.cc
$(CXX) $(CXXFLAGS) -Iinclude -o $# -c $<
Step 3. Add a "do nothing" rule for the library, and verify that it builds all of the objects:
SRC_FILES = $(wildcard $(SRC_DIR)/*.cpp)
OBJECTS=$(patsubst $(SRC_DIR)/%.cpp,$(OBJ_DIR)/%.o,$(SRC_FILES))
libtest.so: $(OBJECTS)
#echo doing nothing
Step 4. Change the library rule to actually build the library:
libtest.so: $(OBJECTS)
$(CXX) -shared -fPIC -o $# $^
Step 5. Add a rule to build the test:
maintest: main.o libtest.so
$(CXX) $(CXXFLAGS) -L. -o $# $< -ltest
Im doing some adition simple program on omniorb 4.2 but the makefile for server gives me an error.
Heres my makeserver file code:
CC = gcc
CPPFLAGS = -g -c
LDFLAGS = -g
OMNI_HOME = /opt/omniorb
OMNI_INCLUDES = -I$(OMNI_HOME)/include
OMNI_LIB_DIR = $(OMNI_HOME)/lib
OMNIIDL = $(OMNI_HOME)/bin/omniidl
INCLUDES = $(OMNI_INCLUDES)
LIBS = -lomniORB4 -lomnithread -lomniDynamic4
OBJECTS = Data.o CServiceA.o Server.o
all Server: $(OBJECTS)
$(CC) $(LDFLAGS) -o Server -L$(OMNI_HOME)/lib $(OBJECTS) $(LIBPATH) $(LIBS)
Data.o: DataSK.cc Data.hh
$(CC) $(CPPFLAGS) $(INCLUDES) DataSK.cc
Server.o: Server.cpp Data.hh
$(CC) $(CPPFLAGS) $(INCLUDES) Server.cpp
CServiceA.o: CServiceA.cpp CServiceA.h Data.hh
$(CC) $(CPPFLAGS) $(INCLUDES) CServiceA.cpp
DataSK.cc: Data.idl
$(OMNI_HOME)/bin/omniidl -bcxx Data.idl
clean clean_all:
rm -fr *.o
rm -fr core
rm -fr *.hh
rm -fr *SK.cc
rm -fr Server
And this is the error it gives me:
$ make -f MakeServer
gcc -g -c -I/opt/omniorb/include DataSK.cc
gcc -g -c -I/opt/omniorb/include CServiceA.cpp
gcc -g -c -I/opt/omniorb/include Server.cpp
gcc -g -o Server -L/opt/omniorb/lib Data.o CServiceA.o Server.o - lomniORB4 -lomnithread -lomniDynamic4
gcc: error: Data.o: file or directory doesn't exist
MakeServer:13: fail in instructions for objective 'all'
make: *** [all] Error 1
The following rule is broken
Data.o: DataSK.cc Data.hh
$(CC) $(CPPFLAGS) $(INCLUDES) DataSK.cc
You've told make that this rule creates Data.o, but it actually creates DataSK.o, so change the rule and OBJECTS
OBJECTS = DataSK.o CServiceA.o Server.o
DataSK.o: DataSK.cc DataSK.hh
$(CC) $(CPPFLAGS) $(INCLUDES) DataSK.cc
As as side note, a lot of your makefile is unnecessary, the built-in rules and gcc dependency generation can cover most of the work:
omni_home := /opt/omniorb
CPPFLAGS := -I$(omni_home)/include -MMD -MP
CXXFLAGS := -g
LDFLAGS := -L$(omni_home)/lib
LDLIBS := -lomniORB4 -lomnithread -lomniDynamic4
objs := DataSK.o CServiceA.o Server.o
deps := $(objs:.o=.d)
.PHONY: all clean
all: Server
Server: CC := g++
Server: $(objs)
DataSK.o: DataSK.cc
DataSK.cc DataSK.hh: Data.idl
$(omni_home)/bin/omniidl -bcxx $<
clean: ; $(RM) $(objs) $(deps) DataSK.cc DataSK.hh Server
-include $(deps)
(The above may not work correctly as I haven't tested it.)
I try to compile a project using clang and libc++. Here is my makefile :
EXEC = ModularMadness
SRCDIR = src/
INC =-I$(SRCDIR)
SOURCES := $(wildcard $(SRCDIR)*.cpp) $(wildcard $(SRCDIR)*/*.cpp)
OBJECTS = $(SOURCES:.cpp=.o)
CXX = clang++
CXX_FLAGS = -std=c++1y -stdlib=libc++ $(INC)
all: $(EXEC)
$(EXEC): $(OBJECTS)
$(CXX) $(OBJECTS) -o $(EXEC)
%.o: %.cpp
$(CXX) -c $(CXX_FLAGS) $< -o $#
.PHONY: all clean
clean:
#echo Cleaning...
#rm -f $(EXEC) $(OBJECTS)
#echo done
The .o files creation works fine, but I ran in multiple error like
In function 'std::__1::weak_ptr<module::Module>::lock() const': undefined reference to 'std::__1::__shared_weak_count::lock()' during linker command.
Could someone help me understanding what's the problem here ?
Note
This makefile run perfectly on OS X.
I missed the linker flag -lc++... Thank you perencia.
Here is the working makefile :
EXEC = ModularMadness
SRCDIR = src/
INC =-I$(SRCDIR)
SOURCES := $(wildcard $(SRCDIR)*.cpp) $(wildcard $(SRCDIR)*/*.cpp)
OBJECTS = $(SOURCES:.cpp=.o)
CXX = clang++
CXX_FLAGS = -std=c++1y -stdlib=libc++ $(INC)
all: $(EXEC)
$(EXEC): $(OBJECTS)
$(CXX) $(OBJECTS) -o $(EXEC) -lc++ # Here
%.o: %.cpp
$(CXX) -c $(CXX_FLAGS) $< -o $#
.PHONY: all clean
clean:
#echo Cleaning...
#rm -f $(EXEC) $(OBJECTS)
#echo done
(On Linux, trying to set up SDL) I'm having a time with makefiles, I'm finding them hard to learn. Here is the error I'm getting.
g++: error: game.exe: No such file or directory
make: *** [game.exe] Error 1
Here is my makefile. (Any suggestions on making it better would be great. I've just kind of slapped together whatever I could find to work.)
#Game Make file
TARGET = game.exe
OBJS = App.o\
App_OnInit.o\
App_OnEvent.o\
App_OnLoop.o\
App_OnRender.o \
App_OnCleanup.o\
SDL_CFLAGS := $(shell sdl-config --cflags)
SDL_LDFLAGS := $(shell sdl-config --libs)
CFLAGS = -Wall -o
LIBS =
LDFLAGS =
$(TARGET): $(OBJS)
g++ $(CFLAGS) $(SDL_CFLAGS) $# $(LDFLAGS) $(OBJS) $(SDL_LDFLAGS) $(LIBS)
%.o: src/%.cpp
g++ -c $(SDL_CFLAGS) $< $(SDL_LDFLAGS)
.PHONY: clean
clean:
rm -f $(TARGET) $(OBJS)
You could either exchange $(CFLAGS) and $(SDL_CFLAGS) in the rule to make $(TARGET) or better remove -o from CFLAGS and put it directly before $#:
...
CFLAGS = -Wall
...
$(TARGET): $(OBJS)
g++ $(CFLAGS) $(SDL_CFLAGS) -o $# $(LDFLAGS) $(OBJS) $(SDL_LDFLAGS) $(LIBS)
-o option should immediately precede the name of the executable file to be produced. In your original Makefile it is part of $(CFLAGS) and is followed by the C flags of the SDL library. Therefore the compiler tries to link in game.exe (the $#) instead of producing an executable file by that name.