I would like to add a cpp file to use the c functions. I made the file but the problem becomes how to include the file in the makefile.All I know is that I should compile with c++ after C. and then link it together.
May someone point me in the right direction.
This is a snippet of testcpp.cpp
extern "C" {
#include "darknet.h"
}
Below is the makefile but i keep getting No rule to make target 'obj/test.cpp', needed by 'darknet'.
GPU=0
CUDNN=0
OPENCV=0
NNPACK=1
NNPACK_FAST=1
ARM_NEON=1
OPENMP=0
DEBUG=0
QPU_GEMM=1
ARCH= -gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=[sm_50,compute_50] \
-gencode arch=compute_52,code=[sm_52,compute_52]
# -gencode arch=compute_20,code=[sm_20,sm_21] \ This one is deprecated?
# This is what I use, uncomment if you know your arch and want to specify
# ARCH= -gencode arch=compute_52,code=compute_52
VPATH=./src/:./examples
SLIB=libdarknet.so
ALIB=libdarknet.a
EXEC=darknet
OBJDIR=./obj/
CC=gcc
NVCC=nvcc
AR=ar
ARFLAGS=rcs
OPTS=-Ofast
LDFLAGS= -lm -pthread
COMMON= -Iinclude/ -Isrc/
#CFLAGS=-Wall -Wno-unknown-pragmas -Wfatal-errors -fPIC
#CFLAGS=-Wall -Wno-unknown-pragmas -Wfatal-errors -fPIC -march=native -mfpmath=sse
CFLAGS=-Wall -Wno-unknown-pragmas -Wfatal-errors -fPIC -mcpu=cortex-a53
ifeq ($(OPENMP), 1)
CFLAGS+= -fopenmp
endif
ifeq ($(DEBUG), 1)
OPTS=-O0 -g
endif
CFLAGS+=$(OPTS)
ifeq ($(OPENCV), 1)
COMMON+= -DOPENCV
CFLAGS+= -DOPENCV
LDFLAGS+= `pkg-config --libs opencv`
COMMON+= `pkg-config --cflags opencv`
endif
ifeq ($(GPU), 1)
COMMON+= -DGPU -I/usr/local/cuda/include/
CFLAGS+= -DGPU
LDFLAGS+= -L/usr/local/cuda/lib64 -lcuda -lcudart -lcublas -lcurand
endif
ifeq ($(CUDNN), 1)
COMMON+= -DCUDNN
CFLAGS+= -DCUDNN
LDFLAGS+= -lcudnn
endif
ifeq ($(QPU_GEMM), 1)
COMMON+= -DQPU_GEMM
CFLAGS+= -DQPU_GEMM
LDFLAGS+= -lqmkl
endif
ifeq ($(NNPACK), 1)
COMMON+= -DNNPACK
CFLAGS+= -DNNPACK
LDFLAGS+= -lnnpack -lpthreadpool
endif
ifeq ($(NNPACK_FAST), 1)
COMMON+= -DNNPACK_FAST
CFLAGS+= -DNNPACK_FAST
endif
ifeq ($(ARM_NEON), 1)
COMMON+= -DARM_NEON
CFLAGS+= -DARM_NEON -mfpu=neon-vfpv4 -funsafe-math-optimizations -ftree-vectorize
endif
OBJ=gemm.o utils.o cuda.o deconvolutional_layer.o convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o detection_layer.o route_layer.o box.o normalization_layer.o avgpool_layer.o layer.o local_layer.o shortcut_layer.o activation_layer.o rnn_layer.o gru_layer.o crnn_layer.o demo.o batchnorm_layer.o region_layer.o reorg_layer.o tree.o lstm_layer.o
EXECOBJA=captcha.o lsd.o super.o art.o tag.o cifar.o go.o rnn.o segmenter.o regressor.o classifier.o coco.o yolo.o detector.o nightmare.o attention.o darknet.o
EXECOBJA1=testcpp.cpp
ifeq ($(GPU), 1)
LDFLAGS+= -lstdc++
OBJ+=convolutional_kernels.o deconvolutional_kernels.o activation_kernels.o im2col_kernels.o col2im_kernels.o blas_kernels.o crop_layer_kernels.o dropout_layer_kernels.o maxpool_layer_kernels.o avgpool_layer_kernels.o
endif
EXECOBJ = $(addprefix $(OBJDIR), $(EXECOBJA))
EXECOBJ1 = $(addprefix $(OBJDIR), $(EXECOBJA1))
OBJS = $(addprefix $(OBJDIR), $(OBJ))
DEPS = $(wildcard src/*.h) Makefile include/darknet.h
CPP=g++
#all: obj backup results $(SLIB) $(ALIB) $(EXEC)
all: obj results $(SLIB) $(ALIB) $(EXEC)
$(EXEC): $(EXECOBJ) $(ALIB)
$(CC) $(COMMON) $(CFLAGS) $^ -o $# $(LDFLAGS) $(ALIB)
$(EXEC): $(EXECOBJ1)
$(CPP) -Wall $^ -o $# $(LDFLAGS)
$(ALIB): $(OBJS)
$(AR) $(ARFLAGS) $# $^
$(SLIB): $(OBJS)
$(CPP) $(CFLAGS) -shared $^ -o $#
$(OBJDIR)%.o: %.c $(DEPS)
$(CC) $(COMMON) $(CFLAGS) -c $< -o $#
$(OBJDIR)%.o: %.cu $(DEPS)
$(NVCC) $(ARCH) $(COMMON) --compiler-options "$(CFLAGS)" -c $< -o $#
obj:
mkdir -p obj
backup:
mkdir -p backup
results:
mkdir -p results
.PHONY: clean
clean:
rm -rf $(OBJS) $(SLIB) $(ALIB) $(EXEC) $(EXECOBJ)
I thought
$(EXEC): $(EXECOBJ1)
$(CPP) -Wall $^ -o $# $(LDFLAGS)
would compile the cpp file and
$(OBJDIR)%.o: %.c $(DEPS)
$(CC) $(COMMON) $(CFLAGS) -c $< -o $#
would link it
I think you need a recipe to compile object files from .cpp file:
$(OBJDIR)%.o: %.cpp $(DEPS)
$(CXX) $(COMMON) $(CFLAGS) -c $< -o $#
The error you describe probably comes from an incorrect usage of vpath: I don't think its usage is necessary here.
But basically I agree with #Basile Starynkevitch : you might be better rewriting from scratch. I have a few suggestions:
1 - Clearly separate in different folders programs (C or C++ files that hold a main() function) and library files (C or C++ files that hold functions). This will be useful for linking.
For example:
|
+--lib // holds "library" code
+--app // holds programs
+--headers
+--BUILD
|
+-- obj
| |
| +-- app
| +-- lib
|
+-- bin
2 - Automatically generate both object files and programs from source files:
SRC_FILES_LIB = $(wildcard lib/*.cpp)
SRC_FILES_APP = $(wildcard app/*.cpp)
OBJ_FILES_LIB = $(patsubst lib/%.cpp,BUILD/obj/lib/%.o,$(SRC_FILES_LIB))
OBJ_FILES_APP = $(patsubst app/%.cpp,BUILD/obj/app/%.o,$(SRC_FILES_APP))
EXEC_FILES = $(patsubst app/%.cpp,BUILD/bin/%,$(SRC_FILES_APP))
3 - add a target that builds the programs:
all: $(EXEC_FILES)
#echo "done"
After that, its just a matter of providing the sufficient pattern rule.
(sorry, no more time for that)
Related
so I have a project with multiple dependencies of the following structure:
Dependencies
- Glad
- GLFW
- ImGui
Engine
Sandbox
Engine includes the dependencies and Sandbox includes Engine
Project files are generated by premake5.
This compiles and runs perfectly under Windows, but today i tried to port it to linux.
I generated makefiles by using the linux version of premake.
All files do compile successfully, but when the linking process starts i get the following error:
==== Building Sandbox (debug) ====
Linking Sandbox
../Engine/vendor/GLFW/bin/Debug-linux-x86_64/GLFW/libGLFW.a(context.o): In function _glfwRefreshContextAttribs':
/home/netcode/Engine/Engine/vendor/GLFW/src/context.c:361: undefined reference to_glfwPlatformGetTls'
../Engine/vendor/GLFW/bin/Debug-linux-x86_64/GLFW/libGLFW.a(context.o): In function glfwMakeContextCurrent':<br>
/home/netcode/Engine/Engine/vendor/GLFW/src/context.c:614: undefined reference to_glfwPlatformGetTls'
../Engine/vendor/GLFW/bin/Debug-linux-x86_64/GLFW/libGLFW.a(context.o): In function glfwGetCurrentContext':
/home/netcode/Engine/Engine/vendor/GLFW/src/context.c:638: undefined reference to_glfwPlatformGetTls'
../Engine/vendor/GLFW/bin/Debug-linux-x86_64/GLFW/libGLFW.a(context.o): In function glfwSwapInterval':
/home/netcode/Engine/Engine/vendor/GLFW/src/context.c:664: undefined reference to_glfwPlatformGetTls'
../Engine/vendor/GLFW/bin/Debug-linux-x86_64/GLFW/libGLFW.a(context.o): In function glfwExtensionSupported':
/home/netcode/Engine/Engine/vendor/GLFW/src/context.c:682: undefined reference to_glfwPlatformGetTls'...
and so on...
this is the final makefile:
# Alternative GNU Make project makefile autogenerated by Premake
ifndef config
config=debug
endif
ifndef verbose
SILENT = #
endif
.PHONY: clean prebuild
SHELLTYPE := posix
ifeq (.exe,$(findstring .exe,$(ComSpec)))
SHELLTYPE := msdos
endif
# Configurations
# #############################################
RESCOMP = windres
INCLUDES += -I../Engine/vendor/spdlog/include -I../Engine/src -I../Engine/vendor -I../Engine/vendor/glm -Isrc
FORCE_INCLUDE +=
ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP $(DEFINES) $(INCLUDES)
ALL_RESFLAGS += $(RESFLAGS) $(DEFINES) $(INCLUDES)
LINKCMD = $(CXX) -o "$#" $(OBJECTS) $(RESOURCES) $(ALL_LDFLAGS) $(LIBS)
define PREBUILDCMDS
endef
define PRELINKCMDS
endef
define POSTBUILDCMDS
endef
ifeq ($(config),debug)
TARGETDIR = ../bin/Debug-linux-x86_64/Sandbox
TARGET = $(TARGETDIR)/Sandbox
OBJDIR = ../bin-int/Debug-linux-x86_64/Sandbox
DEFINES += -DENGINE_DEBUG
ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -m64 -g
ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -m64 -g -std=c++17
LIBS += ../bin/Debug-linux-x86_64/Engine/libEngine.a ../Engine/vendor/GLFW/bin/Debug-linux-x86_64/GLFW/libGLFW.a ../Engine/vendor/Glad/bin/Debug-linux-x86_64/Glad/libGlad.a ../Engine/vendor/imgui/bin/Debug-linux-x86_64/ImGui/libImGui.a -lXrandr -lXi -lGLEW -lGLU -lGL -lX11 -ldl -lpthread -lstdc++fs
LDDEPS += ../bin/Debug-linux-x86_64/Engine/libEngine.a ../Engine/vendor/GLFW/bin/Debug-linux-x86_64/GLFW/libGLFW.a ../Engine/vendor/Glad/bin/Debug-linux-x86_64/Glad/libGlad.a ../Engine/vendor/imgui/bin/Debug-linux-x86_64/ImGui/libImGui.a
ALL_LDFLAGS += $(LDFLAGS) -L/usr/lib64 -m64
else ifeq ($(config),release)
TARGETDIR = ../bin/Release-linux-x86_64/Sandbox
TARGET = $(TARGETDIR)/Sandbox
OBJDIR = ../bin-int/Release-linux-x86_64/Sandbox
DEFINES += -DENGINE_RELEASE
ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -m64 -O2
ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -m64 -O2 -std=c++17
LIBS += ../bin/Release-linux-x86_64/Engine/libEngine.a ../Engine/vendor/GLFW/bin/Release-linux-x86_64/GLFW/libGLFW.a ../Engine/vendor/Glad/bin/Release-linux-x86_64/Glad/libGlad.a ../Engine/vendor/imgui/bin/Release-linux-x86_64/ImGui/libImGui.a -lXrandr -lXi -lGLEW -lGLU -lGL -lX11 -ldl -lpthread -lstdc++fs
LDDEPS += ../bin/Release-linux-x86_64/Engine/libEngine.a ../Engine/vendor/GLFW/bin/Release-linux-x86_64/GLFW/libGLFW.a ../Engine/vendor/Glad/bin/Release-linux-x86_64/Glad/libGlad.a ../Engine/vendor/imgui/bin/Release-linux-x86_64/ImGui/libImGui.a
ALL_LDFLAGS += $(LDFLAGS) -L/usr/lib64 -m64 -s
else ifeq ($(config),dist)
TARGETDIR = ../bin/Dist-linux-x86_64/Sandbox
TARGET = $(TARGETDIR)/Sandbox
OBJDIR = ../bin-int/Dist-linux-x86_64/Sandbox
DEFINES += -DENGINE_DIST
ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -m64 -O2
ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -m64 -O2 -std=c++17
LIBS += ../bin/Dist-linux-x86_64/Engine/libEngine.a ../Engine/vendor/GLFW/bin/Dist-linux-x86_64/GLFW/libGLFW.a ../Engine/vendor/Glad/bin/Dist-linux-x86_64/Glad/libGlad.a ../Engine/vendor/imgui/bin/Dist-linux-x86_64/ImGui/libImGui.a -lXrandr -lXi -lGLEW -lGLU -lGL -lX11 -ldl -lpthread -lstdc++fs
LDDEPS += ../bin/Dist-linux-x86_64/Engine/libEngine.a ../Engine/vendor/GLFW/bin/Dist-linux-x86_64/GLFW/libGLFW.a ../Engine/vendor/Glad/bin/Dist-linux-x86_64/Glad/libGlad.a ../Engine/vendor/imgui/bin/Dist-linux-x86_64/ImGui/libImGui.a
ALL_LDFLAGS += $(LDFLAGS) -L/usr/lib64 -m64 -s
else
$(error "invalid configuration $(config)")
endif
# Per File Configurations
# #############################################
# File sets
# #############################################
OBJECTS :=
OBJECTS += $(OBJDIR)/Sandbox2D.o
OBJECTS += $(OBJDIR)/SandboxApp.o
# Rules
# #############################################
all: $(TARGET)
#:
$(TARGET): $(OBJECTS) $(LDDEPS) | $(TARGETDIR)
$(PRELINKCMDS)
#echo Linking Sandbox
$(SILENT) $(LINKCMD)
$(POSTBUILDCMDS)
$(TARGETDIR):
#echo Creating $(TARGETDIR)
ifeq (posix,$(SHELLTYPE))
$(SILENT) mkdir -p $(TARGETDIR)
else
$(SILENT) mkdir $(subst /,\\,$(TARGETDIR))
endif
$(OBJDIR):
#echo Creating $(OBJDIR)
ifeq (posix,$(SHELLTYPE))
$(SILENT) mkdir -p $(OBJDIR)
else
$(SILENT) mkdir $(subst /,\\,$(OBJDIR))
endif
clean:
#echo Cleaning Sandbox
ifeq (posix,$(SHELLTYPE))
$(SILENT) rm -f $(TARGET)
$(SILENT) rm -rf $(OBJDIR)
else
$(SILENT) if exist $(subst /,\\,$(TARGET)) del $(subst /,\\,$(TARGET))
$(SILENT) if exist $(subst /,\\,$(OBJDIR)) rmdir /s /q $(subst /,\\,$(OBJDIR))
endif
prebuild: | $(OBJDIR)
$(PREBUILDCMDS)
ifneq (,$(PCH))
$(OBJECTS): $(GCH) | $(PCH_PLACEHOLDER)
$(GCH): $(PCH) | prebuild
#echo $(notdir $<)
$(SILENT) $(CXX) -x c++-header $(ALL_CXXFLAGS) -o "$#" -MF "$(#:%.gch=%.d)" -c "$<"
$(PCH_PLACEHOLDER): $(GCH) | $(OBJDIR)
ifeq (posix,$(SHELLTYPE))
$(SILENT) touch "$#"
else
$(SILENT) echo $null >> "$#"
endif
else
$(OBJECTS): | prebuild
endif
# File Rules
# #############################################
$(OBJDIR)/Sandbox2D.o: src/Sandbox2D.cpp
#echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$#" -MF "$(#:%.o=%.d)" -c "$<"
$(OBJDIR)/SandboxApp.o: src/SandboxApp.cpp
#echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$#" -MF "$(#:%.o=%.d)" -c "$<"
-include $(OBJECTS:%.o=%.d)
ifneq (,$(PCH))
-include $(PCH_PLACEHOLDER).d
endif
any guesses?
I'm trying to get dlib library working in my C++ project from last 2 weeks and found some solution but still i'm facing errors which i can not understand. As i am newbie to the makefile and dlib guide me what to do with makefile.
The folder structure is like this:
projectDir
|-makefile
|-src
|-CDetector.cpp
|-CDetectot.h
|-CStreamReader.cpp
|-CStreamReader.h
|-include
|-darknet
|-(files).h (other .h files needed by src files)
|-dlib-19.6
|-all
|-source.cpp
|-lots of header files
|-...
|-external_libs
|-libdarknet.a
|-libdarknet.so
The makefile looks like this:
EXE = darknet
OBJ_DIR = obj
CXXFILES = $(shell find src -type f -name '*.cpp')
CXXOBJ = $(patsubst src/%.cpp,obj/%.o,$(CXXFILES))
INCLUDE = -I/include -I/include/darknet
LIBS = external_libs/libdarknet.a
CXXFLAGS = `pkg-config --cflags-only-I opencv` -Wall -Wno-unknown-pragmas -Wfatal-errors -Wwrite-strings -fPIC
LDFLAGS = -lm -pthread -lX11 -DDLIB_JPEG_SUPPORT -ljpeg
all: $(EXE)
$(EXE): $(CXXOBJ)
$(CXX) $(CXXOBJ) -o $(EXE) $(LIBS) $(LDFLAGS)
$(OBJ_DIR)/%.o: src/%.cpp
$(CXX) $(CXXFLAGS) $(INCLUDE) $< -c -o $#
$(BUILD)
run: all
./$(EXE)
clean:
-rm -f $(EXE) $(CXXOBJ)
rmdir obj/
It needs some modification as INCLUDE, CXXFLAGS AND LDFLAGS will be
INCLUDE = -Iinclude -Iinclude/darknet `pkg-config --cflags-only-I opencv`
CXXFLAGS = -Wall -Wno-unknown-pragmas -Wfatal-errors -Wwrite-strings -fPIC
LDFLAGS = -lm -pthread -lX11 -DDLIB_JPEG_SUPPORT -ljpeg -lopencv_videoio `pkg-config --libs opencv`
and i'm now using darknet makefile to do it so my makefile looks like
VPATH = ./src/
ALIB = external_libs/libdarknet.a
EXEC = darknet
OBJDIR = ./obj/
CC = g++ -std=c++11
NVCC = nvcc
AR = ar
ARFLAGS = rcs
OPTS = -Ofast
LDFLAGS = -lm -pthread -lX11 -DDLIB_JPEG_SUPPORT -ljpeg -lopencv_videoio `pkg-config --libs opencv`
INCLUDE = -Iinclude -Iinclude/darknet `pkg-config --cflags-only-I opencv`
CFLAGS = -Wall -Wno-unknown-pragmas -Wfatal-errors -Wwrite-strings -fPIC
OBJ = CDetector.o CStreamReader.o
EXECOBJA = CDetector.o CStreamReader.o
EXECOBJ = $(addprefix $(OBJDIR), $(EXECOBJA))
OBJS = $(addprefix $(OBJDIR), $(OBJ))
DEPS = $(wildcard src/*.h) Makefile include/darknet/darknet.h
all: obj results $(ALIB) $(EXEC)
$(EXEC): $(EXECOBJ) $(ALIB)
$(CC) $(INCLUDE) $(CFLAGS) $^ -o $# $(LDFLAGS) $(ALIB)
$(ALIB): $(OBJS)
$(AR) $(ARFLAGS) $# $^
$(OBJDIR)%.o: %.cpp $(DEPS)
$(CC) $(INCLUDE) $(CFLAGS) -c $< -o $#
obj:
mkdir -p obj
backup:
mkdir -p backup
results:
mkdir -p results
.PHONY: clean
clean:
rm -rf $(OBJS) $(ALIB) $(EXEC) $(EXECOBJ)
make: *** No rule to make target `glslshaders.o', needed by `openglframework'. Stop.
Who can help?!!
Here is my make file:
# GNU (everywhere)
CC = gcc -g -Wall
# GNU (faster)
#CPP = gcc -O5 -Wall -fomit-frame-pointer -ffast-math
UNAME := $(shell uname)
ifeq ($(UNAME),)
UNAME := MINGW
endif
EXECUTABLE = openglframework
OBJS = openglframework.o glslshaders.o glm.o
ifeq ($(UNAME),Darwin)
LIBS = -lm -framework OpenGL -framework GLUT
else
ifeq ($(findstring MINGW,$(UNAME)),MINGW)
CC += -Iincludes -DNEED_GLEW -DGLEW_STATIC
OBJS += glew.o
LIBS = -lm -lopengl32 -lglu32 -Llibs -lfreeglut
else
LIBS = -lm -lGL -lglut -lGLU
endif
endif
### TARGETS
$(EXECUTABLE): $(OBJS)
$(CC) $(OBJS) $(LIBS) -o $#
depend:
make.dep
clean:
- /bin/rm -f *.bak *~ $(OBJS) $(EXECUTABLE) $(EXECUTABLE).exe
make.dep:
$(CC) -MM $(OBJS:.o=.c) > make.dep
### RULES
.SUFFIXES:
.c .cpp .o .in .ppm .png .jpg .gif
.cpp.o:
$(CC) -c $< .c.o: $(CC) -c $<
### DEPENDENCIES
include make.dep
I am currently trying to build Face Tracker (by Jason Saragih) with "make" in my Windows 7 x64, but all I'm getting is: "No rule to make target src/lib/IO.o, needed by bin/face_tracker".
Does anyone have any idea of what is happening, please?
This is the makefile (also available in Jason Saragih's page):
# Paths
OPENCV_PATH=/OpenCV246/
# Programs
CC=
CXX=g++
# Flags
ARCH_FLAGS=-arch x86_64
CFLAGS=-Wextra -Wall -pedantic-errors $(ARCH_FLAGS) -O3
LDFLAGS=$(ARCH_FLAGS)
DEFINES=
INCLUDES=-I$(OPENCV_PATH)/include -Iinclude/
LIBRARIES=-L$(OPENCV_PATH)/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_objdetect
# Files which require compiling
SOURCE_FILES=\
src/lib/IO.cc\
src/lib/PDM.cc\
src/lib/Patch.cc\
src/lib/CLM.cc\
src/lib/FDet.cc\
src/lib/PAW.cc\
src/lib/FCheck.cc\
src/lib/Tracker.cc
# Source files which contain a int main(..) function
SOURCE_FILES_WITH_MAIN=src/exe/face_tracker.cc
# End Configuration
SOURCE_OBJECTS=$(patsubst %.cc,%.o,$(SOURCE_FILES))
ALL_OBJECTS=\
$(SOURCE_OBJECTS) \
$(patsubst %.cc,%.o,$(SOURCE_FILES_WITH_MAIN))
DEPENDENCY_FILES=\
$(patsubst %.o,%.d,$(ALL_OBJECTS))
all: bin/face_tracker
%.o: %.cc Makefile
## Make dependecy file
$(CXX) -MM -MT $# -MF $(patsubst %.cc,%.d,$<) $(CFLAGS) $(DEFINES) $(INCLUDES) $<
## Compile
$(CXX) $(CFLAGS) $(DEFINES) $(INCLUDES) -c -o $# $<
-include $(DEPENDENCY_FILES)
bin/face_tracker: $(ALL_OBJECTS)
$(CXX) $(LDFLAGS) $(LIBRARIES) -o $# $(ALL_OBJECTS)
.PHONY: clean
clean:
#echo "Cleaning"
#for pattern in '*~' '*.o' '*.d' ; do \
find . -name "$$pattern" | xargs rm ; \
done
Thanks,
Fabio
How do I use different compiler flags for different source files in a Makefile? For example, I'd like a Makefile that will produce:
g++ -c -COMPILER_FLAGS_1 -g source1.cpp -o source1.o
g++ -c -COMPILER_FLAGS_2 -g source2.cpp -o source2.o
g++ -c -COMPILER_FLAGS_2 -g source3.cpp -o source3.o
g++ -c -COMPILER_FLAGS_2 -g source4.cpp -o source4.o
g++ -c -COMPILER_FLAGS_3 -g source5.cpp -o source5.o
g++ -c -COMPILER_FLAGS_3 -g source6.cpp -o source6.o
g++ -c -COMPILER_FLAGS_3 -g source7.cpp -o source7.o
g++ -g -o output source1.o source2.o source3.o source4.o source5.o source6.o source7.o
At the moment I've got about 20 source files (and that's expected to grow), so an easy to maintain file would be nice.
Thanks in advance.
You could do something like the following (untested, so the syntax might be slightly off):
OBJS_1 := source1.o
OBJS_2 := source2.o source3.o source4.o
OBJS_3 := source5.o source6.o source7.o
OBJS := $(OBJS_1) $(OBJS_2) $(OBJS_3)
output: $(OBJS)
$(CXX) -g -o $# $^
$(OBJS_1): CXXFLAGS := $(COMPILER_FLAGS_1)
$(OBJS_2): CXXFLAGS := $(COMPILER_FLAGS_2)
$(OBJS_3): CXXFLAGS := $(COMPILER_FLAGS_3)
$(OBJS): %.o: %.cpp
$(CXX) -c $(CXXFLAGS) -g $< -o $#
Here is UNIX/LINUX makefile which I wrote and tested on Solaris LINUX to handle different compilation flags for different sections of a GNUmakefile. Please let me know if it can be improved. Thank you
# GNUmakefile
#
# makefile for mdRightFielder
#
# Builds:
# libmdRightFielder.so or libmdRightFielder.sl
ifndef SUB
include ../header.mk
else
VPATH=../Source:../Source/PCRE:../Source/SQLite:../../cpswindows/Source:../../util/mdLicense
INCL=-I../Include -I../Include/PCRE -I../Include/SQLite -I../../cpswindows/Include -I ../../util -I../../util/mdLicense
APIOBJ=cGlobalDataDestructor.o cPCRE.o CppInterface.o cRightFielder-FillTokenGaps.o
PCREOBJ=pcre_chartables.o pcre_compile.o pcre_exec.o pcre_fullinfo.o pcre_get.o pcre_globals.o pcre_newline.o \
pcre_tables.o pcre_try_flipped.o
SQLITEOBJ=sqlite3.o
CPSWINDOWSOBJ=BinarySearch.o cConfigFile.o cCriticalSection.o cDateTime.o cException.o cFile.o cSQLite.o \
QuickSort.o StringFunctions.o
MDLICENSEOBJ=CBigNum.o mdLicense.o RSA.o
ifeq ($(CPU),sparc)
ifdef workshop
CALIGN=-xmemalign=1s
ifdef release
CXXALIGN=-Qoption cg -xmemalign=1s
else
CXXALIGN=-Qoption ccfe -y-xmemalign=1s
endif
endif
endif
COMPILER_FLAGS_1=-D_NO_GUI
COMPILER_FLAGS_2=-D_NO_GUI -DHAVE_CONFIG_H
CXXFLAGS+=-DPCRE_STATIC -DUSE_STATIC
CFLAGS+=-D_NO_GUI -DHAVE_CONFIG_H -DPCRE_STATIC -DUSE_STATIC
DEPFLAGS+=-DLINK_SIZE=2 -D_NO_GUI -DHAVE_CONFIG_H -DPCRE_STATIC -DUSE_STATIC
.PHONY: all clean
all: libmdRightFielder.so
cp -fp ../Include/mdRightFielder.h ../../util/mdEnums.h libmdRightFielder.so $(SHIP)
if [ `uname` = HP-UX ] ; \
then \
/bin/mv -f $(SHIP)/libmdRightFielder.so $(SHIP)/libmdRightFielder.sl ; \
fi
clean:
rm -f *.o *.so *.sl deps
rm -f core core.[0-9]*
$(APIOBJ): CXXFLAGS+=$(COMPILER_FLAGS_1)
$(PCREOBJ): CXXFLAGS+=$(COMPILER_FLAGS_2)
MARYOBJS = $(APIOBJ) $(PCREOBJ)
libmdRightFielder.so: \
$(MARYOBJS)
-$(CXX) $(CXXFLAGS) $(INCL) $(SHARED) $^ -o $# $(LDLIBS)
mary:
%.o : %.cpp # cancel implicit CPP compilation rule
%.o : %.cpp
$(CXX) $(CXXFLAGS) $(INCL) $(PIC) $< -o $# -c
%.o : %.c # cancel implicit C compilation rule
%.o : %.c
$(CC) $(CFLAGS) $(INCL) $(PIC) $< -o $# -c
endif