build [folder]
src [filder]
main.cpp
sub_crc [folder]
file.h
file.cpp
Makefile
Makefile
Код:
После компиляции должно получиться следующее:
Код:
build [folder]
obj [folder]
main.o
sub_crc [folder]
file.o
executable
src [filder]
main.cpp
sub_crc [folder]
file.h
file.cpp
Makefile
obj [folder]
main.o
sub_crc [folder]
file.o
executable
src [filder]
main.cpp
sub_crc [folder]
file.h
file.cpp
Makefile
Т.е. необходимо скомпилить "*.cpp" в объектные файлы и положить их в "build/obj" , при чем создав струкруту как в "src". После этого необходимо слинковать полученные "*.o" в исполняемый "executable".
При этом в Makefile имена исходников должны задаваться в виде:
Код:
src_list += main.cpp
src_list += sub_src/main.cpp
src_list += sub_src/main.cpp
Буду благодарен за любую помощь, так как буквально только вчера начал разбираться с Makefile'ами и пока не могу понять как задать необходимые зависимости.
Код:
# Environment
MKDIR=mkdir
CP=cp
CCADMIN=CCadmin
RANLIB=ranlib
# build
build: .build-post
.build-pre:
# Add your pre 'build' code here...
.build-post: .build-impl
# Add your post 'build' code here...
# clean
clean: .clean-post
.clean-pre:
# Add your pre 'clean' code here...
.clean-post: .clean-impl
# Add your post 'clean' code here...
# clobber
clobber: .clobber-post
.clobber-pre:
# Add your pre 'clobber' code here...
.clobber-post: .clobber-impl
# Add your post 'clobber' code here...
# all
all: .all-post
.all-pre:
# Add your pre 'all' code here...
.all-post: .all-impl
# Add your post 'all' code here...
# help
help: .help-post
.help-pre:
# Add your pre 'help' code here...
.help-post: .help-impl
# Add your post 'help' code here...
# include project implementation makefile
include nbproject/Makefile-impl.mk
# include project make variables
include nbproject/Makefile-variables.mk
MKDIR=mkdir
CP=cp
CCADMIN=CCadmin
RANLIB=ranlib
# build
build: .build-post
.build-pre:
# Add your pre 'build' code here...
.build-post: .build-impl
# Add your post 'build' code here...
# clean
clean: .clean-post
.clean-pre:
# Add your pre 'clean' code here...
.clean-post: .clean-impl
# Add your post 'clean' code here...
# clobber
clobber: .clobber-post
.clobber-pre:
# Add your pre 'clobber' code here...
.clobber-post: .clobber-impl
# Add your post 'clobber' code here...
# all
all: .all-post
.all-pre:
# Add your pre 'all' code here...
.all-post: .all-impl
# Add your post 'all' code here...
# help
help: .help-post
.help-pre:
# Add your pre 'help' code here...
.help-post: .help-impl
# Add your post 'help' code here...
# include project implementation makefile
include nbproject/Makefile-impl.mk
# include project make variables
include nbproject/Makefile-variables.mk
Код:
SUB_no=NO
SUBPROJECTS=${SUB_${SUB}}
BUILD_SUBPROJECTS_=.build-subprojects
BUILD_SUBPROJECTS_NO=
BUILD_SUBPROJECTS=${BUILD_SUBPROJECTS_${SUBPROJECTS}}
CLEAN_SUBPROJECTS_=.clean-subprojects
CLEAN_SUBPROJECTS_NO=
CLEAN_SUBPROJECTS=${CLEAN_SUBPROJECTS_${SUBPROJECTS}}
# Project Name
PROJECTNAME=simpleApp
# Active Configuration
DEFAULTCONF=Debug
CONF=${DEFAULTCONF}
# All Configurations
ALLCONFS=Debug Release
# build
.build-impl: .build-pre .validate-impl .depcheck-impl
@#echo "=> Running $@... Configuration=$(CONF)"
${MAKE} -f nbproject/Makefile-${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .build-conf
# clean
.clean-impl: .clean-pre .validate-impl .depcheck-impl
@#echo "=> Running $@... Configuration=$(CONF)"
${MAKE} -f nbproject/Makefile-${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .clean-conf
# clobber
.clobber-impl: .clobber-pre .depcheck-impl
@#echo "=> Running $@..."
for CONF in ${ALLCONFS}; \
do \
${MAKE} -f nbproject/Makefile-$${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .clean-conf; \
done
# all
.all-impl: .all-pre .depcheck-impl
@#echo "=> Running $@..."
for CONF in ${ALLCONFS}; \
do \
${MAKE} -f nbproject/Makefile-$${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .build-conf; \
done
# dependency checking support
.depcheck-impl:
@echo "# This code depends on make tool being used" >.dep.inc
@if [ -n "${MAKE_VERSION}" ]; then \
echo "DEPFILES=\$$(wildcard \$$(addsuffix .d, \$${OBJECTFILES}))" >>.dep.inc; \
echo "ifneq (\$${DEPFILES},)" >>.dep.inc; \
echo "include \$${DEPFILES}" >>.dep.inc; \
echo "endif" >>.dep.inc; \
else \
echo ".KEEP_STATE:" >>.dep.inc; \
echo ".KEEP_STATE_FILE:.make.state.\$${CONF}" >>.dep.inc; \
fi
# configuration validation
.validate-impl:
@if [ ! -f nbproject/Makefile-${CONF}.mk ]; \
then \
echo ""; \
echo "Error: can not find the makefile for configuration '${CONF}' in project ${PROJECTNAME}"; \
echo "See 'make help' for details."; \
echo "Current directory: " `pwd`; \
echo ""; \
fi
@if [ ! -f nbproject/Makefile-${CONF}.mk ]; \
then \
exit 1; \
fi
# help
.help-impl: .help-pre
@echo "This makefile supports the following configurations:"
@echo " ${ALLCONFS}"
@echo ""
@echo "and the following targets:"
@echo " build (default target)"
@echo " clean"
@echo " clobber"
@echo " all"
@echo " help"
@echo ""
@echo "Makefile Usage:"
@echo " make [CONF=<CONFIGURATION>] [SUB=no] build"
@echo " make [CONF=<CONFIGURATION>] [SUB=no] clean"
@echo " make [SUB=no] clobber"
@echo " make [SUB=no] all"
@echo " make help"
@echo ""
@echo "Target 'build' will build a specific configuration and, unless 'SUB=no',"
@echo " also build subprojects."
@echo "Target 'clean' will clean a specific configuration and, unless 'SUB=no',"
@echo " also clean subprojects."
@echo "Target 'clobber' will remove all built files from all configurations and,"
@echo " unless 'SUB=no', also from subprojects."
@echo "Target 'all' will will build all configurations and, unless 'SUB=no',"
@echo " also build subprojects."
@echo "Target 'help' prints this message."
@echo ""
SUBPROJECTS=${SUB_${SUB}}
BUILD_SUBPROJECTS_=.build-subprojects
BUILD_SUBPROJECTS_NO=
BUILD_SUBPROJECTS=${BUILD_SUBPROJECTS_${SUBPROJECTS}}
CLEAN_SUBPROJECTS_=.clean-subprojects
CLEAN_SUBPROJECTS_NO=
CLEAN_SUBPROJECTS=${CLEAN_SUBPROJECTS_${SUBPROJECTS}}
# Project Name
PROJECTNAME=simpleApp
# Active Configuration
DEFAULTCONF=Debug
CONF=${DEFAULTCONF}
# All Configurations
ALLCONFS=Debug Release
# build
.build-impl: .build-pre .validate-impl .depcheck-impl
@#echo "=> Running $@... Configuration=$(CONF)"
${MAKE} -f nbproject/Makefile-${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .build-conf
# clean
.clean-impl: .clean-pre .validate-impl .depcheck-impl
@#echo "=> Running $@... Configuration=$(CONF)"
${MAKE} -f nbproject/Makefile-${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .clean-conf
# clobber
.clobber-impl: .clobber-pre .depcheck-impl
@#echo "=> Running $@..."
for CONF in ${ALLCONFS}; \
do \
${MAKE} -f nbproject/Makefile-$${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .clean-conf; \
done
# all
.all-impl: .all-pre .depcheck-impl
@#echo "=> Running $@..."
for CONF in ${ALLCONFS}; \
do \
${MAKE} -f nbproject/Makefile-$${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .build-conf; \
done
# dependency checking support
.depcheck-impl:
@echo "# This code depends on make tool being used" >.dep.inc
@if [ -n "${MAKE_VERSION}" ]; then \
echo "DEPFILES=\$$(wildcard \$$(addsuffix .d, \$${OBJECTFILES}))" >>.dep.inc; \
echo "ifneq (\$${DEPFILES},)" >>.dep.inc; \
echo "include \$${DEPFILES}" >>.dep.inc; \
echo "endif" >>.dep.inc; \
else \
echo ".KEEP_STATE:" >>.dep.inc; \
echo ".KEEP_STATE_FILE:.make.state.\$${CONF}" >>.dep.inc; \
fi
# configuration validation
.validate-impl:
@if [ ! -f nbproject/Makefile-${CONF}.mk ]; \
then \
echo ""; \
echo "Error: can not find the makefile for configuration '${CONF}' in project ${PROJECTNAME}"; \
echo "See 'make help' for details."; \
echo "Current directory: " `pwd`; \
echo ""; \
fi
@if [ ! -f nbproject/Makefile-${CONF}.mk ]; \
then \
exit 1; \
fi
# help
.help-impl: .help-pre
@echo "This makefile supports the following configurations:"
@echo " ${ALLCONFS}"
@echo ""
@echo "and the following targets:"
@echo " build (default target)"
@echo " clean"
@echo " clobber"
@echo " all"
@echo " help"
@echo ""
@echo "Makefile Usage:"
@echo " make [CONF=<CONFIGURATION>] [SUB=no] build"
@echo " make [CONF=<CONFIGURATION>] [SUB=no] clean"
@echo " make [SUB=no] clobber"
@echo " make [SUB=no] all"
@echo " make help"
@echo ""
@echo "Target 'build' will build a specific configuration and, unless 'SUB=no',"
@echo " also build subprojects."
@echo "Target 'clean' will clean a specific configuration and, unless 'SUB=no',"
@echo " also clean subprojects."
@echo "Target 'clobber' will remove all built files from all configurations and,"
@echo " unless 'SUB=no', also from subprojects."
@echo "Target 'all' will will build all configurations and, unless 'SUB=no',"
@echo " also build subprojects."
@echo "Target 'help' prints this message."
@echo ""
Код:
CND_BASEDIR=`pwd`
CND_BUILDDIR=build
CND_DISTDIR=dist
# Debug configuration
CND_PLATFORM_Debug=None-Solaris-x86
CND_ARTIFACT_DIR_Debug=dist/Debug/None-Solaris-x86
CND_ARTIFACT_NAME_Debug=simpleapp
CND_ARTIFACT_PATH_Debug=dist/Debug/None-Solaris-x86/simpleapp
CND_PACKAGE_DIR_Debug=dist/Debug/None-Solaris-x86/package
CND_PACKAGE_NAME_Debug=simpleapp.tar
CND_PACKAGE_PATH_Debug=dist/Debug/None-Solaris-x86/package/simpleapp.tar
# Release configuration
CND_PLATFORM_Release=None-Solaris-x86
CND_ARTIFACT_DIR_Release=dist/Release/None-Solaris-x86
CND_ARTIFACT_NAME_Release=simpleapp
CND_ARTIFACT_PATH_Release=dist/Release/None-Solaris-x86/simpleapp
CND_PACKAGE_DIR_Release=dist/Release/None-Solaris-x86/package
CND_PACKAGE_NAME_Release=simpleapp.tar
CND_PACKAGE_PATH_Release=dist/Release/None-Solaris-x86/package/simpleapp.tar
CND_BUILDDIR=build
CND_DISTDIR=dist
# Debug configuration
CND_PLATFORM_Debug=None-Solaris-x86
CND_ARTIFACT_DIR_Debug=dist/Debug/None-Solaris-x86
CND_ARTIFACT_NAME_Debug=simpleapp
CND_ARTIFACT_PATH_Debug=dist/Debug/None-Solaris-x86/simpleapp
CND_PACKAGE_DIR_Debug=dist/Debug/None-Solaris-x86/package
CND_PACKAGE_NAME_Debug=simpleapp.tar
CND_PACKAGE_PATH_Debug=dist/Debug/None-Solaris-x86/package/simpleapp.tar
# Release configuration
CND_PLATFORM_Release=None-Solaris-x86
CND_ARTIFACT_DIR_Release=dist/Release/None-Solaris-x86
CND_ARTIFACT_NAME_Release=simpleapp
CND_ARTIFACT_PATH_Release=dist/Release/None-Solaris-x86/simpleapp
CND_PACKAGE_DIR_Release=dist/Release/None-Solaris-x86/package
CND_PACKAGE_NAME_Release=simpleapp.tar
CND_PACKAGE_PATH_Release=dist/Release/None-Solaris-x86/package/simpleapp.tar
Код:
# Environment
MKDIR=mkdir
CP=cp
CCADMIN=CCadmin
RANLIB=ranlib
CC=
CCC=
CXX=
FC=
AS=
# Macros
CND_PLATFORM=None-Solaris-x86
CND_CONF=Debug
CND_DISTDIR=dist
# Include project Makefile
include Makefile
# Object Directory
OBJECTDIR=build/${CND_CONF}/${CND_PLATFORM}
# Object Files
OBJECTFILES= \
${OBJECTDIR}/main.o
# C Compiler Flags
CFLAGS=
# CC Compiler Flags
CCFLAGS=
CXXFLAGS=
# Fortran Compiler Flags
FFLAGS=
# Assembler Flags
ASFLAGS=
# Link Libraries and Options
LDLIBSOPTIONS=
# Build Targets
.build-conf: ${BUILD_SUBPROJECTS}
${MAKE} -f nbproject/Makefile-Debug.mk dist/Debug/None-Solaris-x86/simpleapp
dist/Debug/None-Solaris-x86/simpleapp: ${OBJECTFILES}
${MKDIR} -p dist/Debug/None-Solaris-x86
${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/simpleapp ${OBJECTFILES} ${LDLIBSOPTIONS}
${OBJECTDIR}/main.o: nbproject/Makefile-${CND_CONF}.mk main.cpp
${MKDIR} -p ${OBJECTDIR}
${RM} $@.d
$(COMPILE.cc) -g -MMD -MP -MF $@.d -o ${OBJECTDIR}/main.o main.cpp
# Subprojects
.build-subprojects:
# Clean Targets
.clean-conf: ${CLEAN_SUBPROJECTS}
${RM} -r build/Debug
${RM} dist/Debug/None-Solaris-x86/simpleapp
# Subprojects
.clean-subprojects:
# Enable dependency checking
.dep.inc: .depcheck-impl
include .dep.inc
MKDIR=mkdir
CP=cp
CCADMIN=CCadmin
RANLIB=ranlib
CC=
CCC=
CXX=
FC=
AS=
# Macros
CND_PLATFORM=None-Solaris-x86
CND_CONF=Debug
CND_DISTDIR=dist
# Include project Makefile
include Makefile
# Object Directory
OBJECTDIR=build/${CND_CONF}/${CND_PLATFORM}
# Object Files
OBJECTFILES= \
${OBJECTDIR}/main.o
# C Compiler Flags
CFLAGS=
# CC Compiler Flags
CCFLAGS=
CXXFLAGS=
# Fortran Compiler Flags
FFLAGS=
# Assembler Flags
ASFLAGS=
# Link Libraries and Options
LDLIBSOPTIONS=
# Build Targets
.build-conf: ${BUILD_SUBPROJECTS}
${MAKE} -f nbproject/Makefile-Debug.mk dist/Debug/None-Solaris-x86/simpleapp
dist/Debug/None-Solaris-x86/simpleapp: ${OBJECTFILES}
${MKDIR} -p dist/Debug/None-Solaris-x86
${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/simpleapp ${OBJECTFILES} ${LDLIBSOPTIONS}
${OBJECTDIR}/main.o: nbproject/Makefile-${CND_CONF}.mk main.cpp
${MKDIR} -p ${OBJECTDIR}
${RM} $@.d
$(COMPILE.cc) -g -MMD -MP -MF $@.d -o ${OBJECTDIR}/main.o main.cpp
# Subprojects
.build-subprojects:
# Clean Targets
.clean-conf: ${CLEAN_SUBPROJECTS}
${RM} -r build/Debug
${RM} dist/Debug/None-Solaris-x86/simpleapp
# Subprojects
.clean-subprojects:
# Enable dependency checking
.dep.inc: .depcheck-impl
include .dep.inc
Я тут набросал элементарный пример
Код:
bin_target = myProject
bin_srcs += main.cpp
bin_srcs += sub_src/file.cpp
src_dir = src
build_dir = build
#==============#
bin_objs = $(bin_srcs:%.cpp=$(src_dir)/%.o)
all: build
build: $(bin_objs)
g++ $(bin_objs) -o $(build_dir)/$(bin_target)
%.o: %.cpp
g++ -c $< -o $@
clean:
rm -f $(bin_objs) $(build_dir)/$(bin_target)
bin_srcs += main.cpp
bin_srcs += sub_src/file.cpp
src_dir = src
build_dir = build
#==============#
bin_objs = $(bin_srcs:%.cpp=$(src_dir)/%.o)
all: build
build: $(bin_objs)
g++ $(bin_objs) -o $(build_dir)/$(bin_target)
%.o: %.cpp
g++ -c $< -o $@
clean:
rm -f $(bin_objs) $(build_dir)/$(bin_target)
В данном случае все "*.o" файлы складываются вместе с исходниками. Подскажите пожалуйста как составить правила, чтобы они складывались в "build/obj"?
Код:
OBJECTDIR=build/obj
MKDIR=mkdir
sources = main.c
dir = sub_src
$(dir)_sources := $(wildcard $(dir)/*.c)
objects = main.o
main.o : $(sources) $($(dir)_sources)
mainPrg : $(objects)
${MKDIR} -p $(OBJECTDIR)
cc mainPrg -o $(OBJECTDIR)/$(objects)
MKDIR=mkdir
sources = main.c
dir = sub_src
$(dir)_sources := $(wildcard $(dir)/*.c)
objects = main.o
main.o : $(sources) $($(dir)_sources)
mainPrg : $(objects)
${MKDIR} -p $(OBJECTDIR)
cc mainPrg -o $(OBJECTDIR)/$(objects)
тестировать не буду. если не получится, то можно по другому
Код:
objects = main.o
mainPrg : $(objects)
${MKDIR} -p $(OBJECTDIR)
cc mainPrg -o $(OBJECTDIR)/$(objects) $(sources) $($(dir)_sources)
mainPrg : $(objects)
${MKDIR} -p $(OBJECTDIR)
cc mainPrg -o $(OBJECTDIR)/$(objects) $(sources) $($(dir)_sources)