1

I am trying to build a CMake file that replicates a Makefile that I have always used. My professor gave us a working CMakeLists.txt that I have been able to use but I was trying to customize it so it would act the same as my Makefile since I use different flags for each build.

Here is my Makefile:

OBJS = Assign1.cpp 

CC = g++
DEBUG_FLAGS = -O0 -g3 -fsanitize=address
RELEASE_FLAGS = -O3 -g0
FLAGS = -std=c++14 -Wall -Wextra

release: $(OBJS)
        $(CC) $(RELEASE_FLAGS) $(FLAGS) $(OBJS) -o Assign1

debug: $(OBJS) 
        $(CC) $(DEBUG_FLAGS) $(FLAGS) $(OBJS) -o debug && cgdb ./debug

In the command line, I simply type make and it automatically defaults to release. If I type make debug it will make the debug and run cgdb on the executable.

I have tried doing the same with my CMake File but I do not know how to set up/run different executables if that is the correct terminology. This is my Cmake File:

cmake_minimum_required(VERSION 3.5)
project(Assignment2)

set(RELEASE "-O3 -g0")
set(DEBUG "-O0 -g3 -fsanitize=address")
set(CMAKE_CXX_FLAGS "-std=c++14 -Wall -Wextra")

set(SOURCE_FILES main.cpp compute-e.hpp compute-e.cpp compute-fib.cpp compute-fib.hpp compute-pi.cpp compute-pi.hpp)

add_executable(Assign2 ${RELEASE} ${CMAKE_CXX_FLAGS} ${SOURCE_FILES})
add_executable(debug ${DEBUG} ${CMAKE_CXX_FLAGS} ${SOURCE_FILES} && cgdb ./debug)

What do I need to do to get this to work? I understand Makefiles better than I do CMake files.

Sailanarmo
  • 1,139
  • 15
  • 41
  • 3
    Debug and release should be built in separate *directories* rather than different rules in the same directory. Look up "out of tree builds". – o11c Sep 13 '17 at 04:20
  • Possible duplicate of [Debug vs Release in CMAKE](https://stackoverflow.com/questions/7724569/debug-vs-release-in-cmake) – Florian Sep 13 '17 at 08:11

0 Answers0