0

In my makefile I used the option

-MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" 

so the compiler will search for the dependencies on its own. How can I establish this with cmake? In general I have huge problems to convert my makefile to the cmake syntax. This is what I achieved until now:

makefile:

################################################################################
# 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)" " )

My next step is to use cmake for platform independence, but first I want to understand how this works here (linux).

varantir
  • 6,624
  • 6
  • 36
  • 57

0 Answers0