0

I would like to ask how to convert Makefile to CMAKEList. Currently I can compile CMAKEList. However, the program doesn’t behave like the program generated out of Makefile program.

details: 1. This is my Makefile

#===========================================================================
# Makefile for behavior_program
#---------------------------------------------------------------------------
# [Update log]
#===========================================================================

TARGET = PFforAEV0.2.2
CC = g++
SYSTEM = linux
LIB_DIR = -L/usr/local/lib
#LIBS    = -lglut -lGLU -lGL -lm -lX11 -lXi -L/usr/X11R6/lib -lpthread -lSSM
LIBS    =  -lm  -L/usr/X11R6/lib -L/usr/local/lib64 -lpthread -lSSM
INCDIR = -I./include -I/usr/local/include 

#DEBUG
CPPFLAGS= -O3 -Wall  $(INCDIR)

#COMPLETE
#CFLAGS = -O2 -g -Wall -Werror -Wmissing-prototypes $(INCDIR)

OBJS = primitives.o PFforAEV.o motion_vw.o observation_lrf.o particle_filter.o gnuplot2D.o

all: $(SYSTEM)

linux: $(OBJS)
 $(CC) $(OBJS) $(LIB_DIR) $(LIBS) -o $(TARGET) 
 rm -f  *.o *~ defs/*~ core

clean:
 rm -f $(TARGET) *.o *~ defs/*~ core 

install:
 cp $(TARGET) ../../bin
  1. This is my CMAKEList

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(PFforAEV0.2.2)
include_directories( -I./include -I/usr/local/include) 
include_directories(-I./include -I/usr/local/include)

add_executable(PFforAEV0.2.2   primitives.cpp PFforAEV.cpp motion_vw.cpp 
observation_lrf.cpp particle_filter.cpp gnuplot2D.cpp)

target_link_libraries (PFforAEV0.2.2  -lm  -L/usr/X11R6/lib -L/usr/local/lib64 -lpthread -lSSM  -L/usr/local/lib)   

set(CMAKE_CXX_FLAGS " -Wall -I./include -I/usr/local/include ")
tux3
  • 7,171
  • 6
  • 39
  • 51

2 Answers2

3

From a quick look, your compile flags are not the same.

For example, in the Makefile you have

CPPFLAGS= -O3 -Wall  $(INCDIR)

But, in CMakeLists.txt you have

set(CMAKE_CXX_FLAGS " -Wall -I./include -I/usr/local/include ")

which is missing the "-O3" flag.

Make sure the flags are identical.

The same goes for the linked libraries.

bendervader
  • 2,619
  • 2
  • 19
  • 22
0

I just like to hint that there are some mechanisms/commands in CMake you could use to reduce your codes dependency on certain compiler options and installation environments:

cmake_minimum_required( VERSION 2.8 FATAL_ERROR )
project( PFforAEV0.2.2 C++ )

find_package( Threads )

include_directories( 
   include 
   usr/local/include
) 

set(
   inFiles
   primitives.cpp 
   PFforAEV.cpp 
   motion_vw.cpp 
   observation_lrf.cpp 
   particle_filter.cpp 
   gnuplot2D.cpp
)

add_definitions(" -Wall -O3")
add_executable( PFforAEV0.2.2 ${inFiles} )

link_directories(
   /usr/X11R6/lib 
   /usr/local/lib64
   /usr/local/lib
)

target_link_libraries(
    PFforAEV0.2.2 
    ${CMAKE_THREAD_LIBS_INIT} 
    m  
    SSM
)

See also:

You may not need all include or library search paths, because CMake also detects the environment e.g. by checking environments variables:

// see share/cmake-2.8/Modules/CMakeCommonLanguageInclude.cmake
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS_INIT} $ENV{LDFLAGS}" CACHE STRING "Flags used by the linker.")

// see share/cmake-2.8/Modules/CMakeCXXInformation.cmake
set(CMAKE_CXX_FLAGS_INIT "$ENV{CXXFLAGS} ${CMAKE_CXX_FLAGS_INIT}")

And CMake does handle the different debug/optimization options for you in different build configs. E.g. GNU C++ defaults look like this:

set(CMAKE_CXX_FLAGS_INIT "")
set(CMAKE_CXX_FLAGS_DEBUG_INIT "-g")
set(CMAKE_CXX_FLAGS_MINSIZEREL_INIT "-Os -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE_INIT "-O3 -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "-O2 -g -DNDEBUG")
Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149