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.)
Related
I'm new on using Makefiles because I've been programming with VS2019 on Windows, solving all my compilation and linking problems.
This is the result:
BUILD_DIR:= ./build
SRC_DIRS := ./src
INCL_DIR := ./includes
CC := /usr/bin/g++ #Compiler used
LVERSION := -std=c++17 #Language Version
CXXFLAGS := -g #Cpp flags
CFLAGS := -Wall #Compiler Flags
LFLAGS := -lstdc++fs #Linker Flags
SRCS := Audio.cpp Song.cpp Visual.cpp VisualSong.cpp
LIBS :=
INCLUDES := $(SRCS:%.cpp=$(INCL_DIR)/%.h)
OBJS := $(SRCS:%.cpp=$(BUILD_DIR)/%.o)
PROG := progName.exe
progName: $(OBJS)
$(CC) $(CFLAGS) $(CXXFLAGS) -o $(INCLUDES) $(PROG) $(OBJS)
$(BUILD_DIR)/%.o: $(INCL_DIR)/%.h $(SRC_DIRS)/%.cpp
$(CC) ${CFLAGS} $(CXXFLAGS) $(LVERSION) ${LFLAGS} -c $^
.PHONY: progName
clean:
/bin/rm -rf build/*.o $(PROG) includes/*.gch
This makefile works until is trying to look on objects file, supposedly created on build directory but, in the end, they're created in Makefile's directory, which is an inconvenient since all what i want is to have separated files for organization purposes.
I know that somehow using implicit rules that are using the dst's directory should do the trick, but i think that I'm missing something on the way...
I'm on a Windows 10 machine with WSL for Ubuntu, but this shouldn't be a problem at all for this problem.
Could anyone explain to me what am I missing?
Look at this rule:
$(BUILD_DIR)/%.o: $(INCL_DIR)/%.h $(SRC_DIRS)/%.cpp
$(CC) ${CFLAGS} $(CXXFLAGS) $(LVERSION) ${LFLAGS} -c $^
Ostensibly it is the rule to build build/foo.o, but the recipe ($(CC)...) actually builds foo.o. There is an easy fix:
$(BUILD_DIR)/%.o: $(INCL_DIR)/%.h $(SRC_DIRS)/%.cpp
$(CC) ${CFLAGS} $(CXXFLAGS) $(LVERSION) ${LFLAGS} -c $^ -o $#
Once that works I suggest you make one further change:
$(BUILD_DIR)/%.o: $(SRC_DIRS)/%.cpp $(INCL_DIR)/%.h
$(CC) ${CFLAGS} $(CXXFLAGS) $(LVERSION) ${LFLAGS} -c $< -o $#
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
I'm trying to compile my program C using makefile in ubuntu. But I dont know whats the problem in it. And there is an error which I can't fix.
gcc -Wall -I. -pthread -ggdb -g -O0 -o bin/server server/message_queue.o server/client_thread.o server/server.o server/file.o server/datatypes.o common/datatypes.o common/error.o common/socket.o
/usr/bin/ld: cannot open output file bin/server: No such file or directory
collect2: error: ld returned 1 exit status
make: *** [bin/server] Error 1
here is a makefile:
CC=gcc
CFLAGS=-Wall -I. -pthread -ggdb -g -O0
SERVER_OBJ=\
server/message_queue.o \
server/client_thread.o \
server/server.o \
server/file.o \
server/datatypes.o
COMMON_OBJ=\
common/datatypes.o \
common/error.o \
common/socket.o
CLIENT_OBJ=\
client/send_thread.o \
client/recv_thread.o \
client/terminal_thread.o \
client/client.o \
client/datatypes.o
BIN=bin
all: server client
server: $(BIN)/server
client: $(BIN)/client
$(BIN)/server: $(SERVER_OBJ) $(COMMON_OBJ)
$(CC) $(CFLAGS) $(SERVER_CFLAGS) -o $# $^
$(BIN)/client: $(CLIENT_OBJ) $(COMMON_OBJ)
$(CC) $(CFLAGS) $(CLIENT_CFLAGS) -o $# $^
%.o: %.c
$(CC) $(CFLAGS) -c -o $# $^
clean:
rm -f server
May be I forgot to install somthing for linux?
Your problem is simple: you do not check if the directory bin exists before linking your executables. Also, your makefile is a little bit messy. That one should do what you want:
BIN := bin
CLIENT := $(BIN)/client
SERVER := $(BIN)/server
COMMON_SRC := $(wildcard common/*.c)
COMMON_OBJ := $(COMMON_SRC:.c=.o)
CLIENT_SRC := $(wildcard client/*.c)
CLIENT_OBJ := $(CLIENT_SRC:.c=.o)
SERVER_SRC := $(wildcard server/*.c)
SERVER_OBJ := $(SERVER_SRC:.c=.o)
CPPFLAGS := -I. -pthread
CFLAGS := -Wall -ggdb -g -O0
LDFLAGS := -pthread
.PHONY: all client server clean fclean
all: client server
client: $(CLIENT)
server: $(SERVER)
$(CLIENT): $(COMMON_OBJ) $(CLIENT_OBJ) | $(BIN)
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $#
$(SERVER): $(COMMON_OBJ) $(SERVER_OBJ) | $(BIN)
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $#
$(BIN):
mkdir $#
clean:
#$(RM) -rv $(BIN) $(COMMON_OBJ) $(CLIENT_OBJ) $(SERVER_OBJ)
So I can compile my code (fftw_ex.c) directly with:
login$ gcc -o -g fftw_ex fftw_ex.c -I$TACC_FFTW3_INC -L$TACC_FFTW3_LIB -lfftw3
However, my professor prefers that we use a Makefile. I am just learning how to use Makefile and make, and I'm having trouble creating the Makefile. So far, this is what I have:
# RULES
EXEC := fftw_ex
SRC := $(wildcard *.c)
OBJ := $(patsubst %.c,%.o,%(SRC))
# OPERATIONS
CC := gcc
CFLAGS := -O3 -I$TACC_FFTW3_INC
LDFLAGS := -L$TACC_FFTW3_LIB
LDLIBS := -lfftw3
$(EXEC): $(OBJ)
$(CC) $(LDFLAGS) $(LDLIBS) -o -g $# $^
%.o: %.c
$(CC) $(CFLAGS) -c $<
# PHONY TARGETS
.PHONY: clean
clean:
#echo Cleaning...;rm -rf *.o fftw_ex
I know there's a problem with the SRC line, as i'm getting the error message:
make: *** No rule to make target `%(SRC)', needed by `fftw_ex'. Stop.
Any help to get this to work would be appreciated.
1)To resolve:
No rule to make target `%(SRC)'
replace %(SRC) in
OBJ := $(patsubst %.c,%.o,%(SRC))
with $(SRC)
2)In line:
$(CC) $(LDFLAGS) $(LDLIBS) -o -g $# $^
you have mistake: -o -g, should be -g -o
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)