在我的makefile中,我使用了选项
-MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)"
因此编译器将自己搜索依赖项。我怎样才能用cmake建立这个?一般来说,我将makefile转换为cmake语法存在很大问题。这是我到目前为止所取得的成就:
生成文件:
################################################################################
# VARIABLES
################################################################################
# Compiler
CC = g++
# Used libs
LIBS = -lmylib
# Directories
SRC_DIR = ../src
BIN_DIR = ./build
LIB_DIR = ../../../libs
# libACI and libbase includes
INCDIRS = -I$(LIB_DIR)/libmylib/public/include
# libraries: .so files
LIBDIRS = -L$(LIB_DIR)/libmylib/public/lib/
# executable name (and directory if needed)
BIN_NAME = ./test1
# Add inputs and outputs from these tool invocations to the build variables
FILES = load_mylib.cpp test1.cpp
################################################################################
# GENERAL
################################################################################
SRC = $(addprefix $(SRC_DIR)/, $(FILES))
OBJ = $(patsubst $(SRC_DIR)/%.cpp,$(BIN_DIR)/%.o,$(SRC))
# This is only in order to delete all the stuff in clean
DEP = $(patsubst $(SRC_DIR)/%.cpp,$(BIN_DIR)/%.d,$(SRC))
# Flags for compiling each file
CFLAGS1 = -O0 -g3 -Wall -c -fmessage-length=0 -lpthread
DEPFLAGS = -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)"
# Each subdirectory must supply rules for building sources it contributes
$(BIN_DIR)/%.o: $(SRC_DIR)/%.cpp
@echo 'found rule'
@echo 'Building file: $<'
@echo 'Invoking: GCC C++ Compiler'
$(CC) $(INCDIRS) $(CFLAGS1) $(DEPFLAGS) -o "$@" "$<"
@echo 'Finished building: $<'
@echo ' '
# main Target
all: main_target
# Tool invocations
main_target: $(OBJ)
@echo 'Building target: $@'
@echo 'Invoking: GCC C++ Linker'
g++ $(LIBDIRS) -o $(BIN_NAME) $(OBJ) $(LIBS)
@echo 'Finished building target: $@'
@echo ' '
# Other Targets
RM := rm -rf
clean:
-$(RM) $(OBJ) $(DEP) $(BIN_NAME)
-@echo ' '
.PHONY: all clean dependents
.SECONDARY:
cmake CMakeLists.txt:
project (test_cmake)
message("Starting to build project test_cmake.")
set(BIN_DIR "../bin")
set(LIB_DIR "../../../libs")
include_directories(${CMAKE_BINARY_DIR}${LIB_DIR}"/libmylib/public/include" ${CMAKE_BINARY_DIR}${LIB_DIR}"/libmylib2/public/include")
message("libraries are linked to"${CMAKE_BINARY_DIR}/${LIB_DIR})
add_executable (test1 load_mylib.cpp testl1.cpp)
target_link_libraries(test_cmake libmylib)
set( COMPILE_FLAGS "-O0 -g3 -Wall -c -fmessage-length=0 -lpthread -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" " )
我的下一步是使用cmake实现平台独立性,但首先我要了解这是如何工作的(linux)。