将Makefile转换为CMAKEList(编译成功但程序行为不同)

时间:2015-06-05 05:04:35

标签: c++ makefile cmake

我想问一下如何将Makefile转换为CMAKEList 。目前我可以编译CMAKEList。但是,该程序的行为与Makefile程序生成的程序不同。

细节: 1.这是我的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. 这是我的CMAKEList
  2. 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 ")
    

2 个答案:

答案 0 :(得分:3)

从快速看,你的编译标志不一样。

例如,在Makefile中有

CPPFLAGS= -O3 -Wall  $(INCDIR)

但是,在CMakeLists.txt中你有

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

缺少" -O3"标志。

确保标志相同。

链接库也是如此。

答案 1 :(得分:0)

我只想暗示CMake中有一些机制/命令可以用来减少代码对某些编译器选项和安装环境的依赖性:

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
)

另见:

您可能不需要所有包含或库搜索路径,因为CMake还会检测环境,例如通过检查环境变量:

// 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}")

CMake会在不同的构建配置中为您处理不同的调试/优化选项。例如。 GNU C ++默认值如下所示:

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