Cmake 编译设置

问题描述

利用编写的CmakeLists文件编译链接fortran编写函数库与C++编写的geant4程序;需要解决的问题有:

1.如何设置环境变量;

2.如何利用CmakeLists编译fortran代码;

3.如何在C++中调用fortran编写的函数;

代码实现

1.环境变量设置

export PATH=$PATH::$PWD/bin #设置执行文件安装路径

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$(pwd)/lib #设置库文件路径

2.如何利用CmakeLists编译fortran代码;

#----------------------------------------------------------------------------
# Setup the project
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
set(PROJ_NAME FortranEarthAtmosphere)
project($PROJ_NAME)
enable_language(Fortran)
set(CMAKE_BUILD_TYPE Release)

#----------------------------------------------------------------------------
# Locate sources and headers for this project
# NB: headers are included so they will show up in IDEs
#

file(GLOB_RECURSE sources  ${PROJECT_SOURCE_DIR}/*.f*)


add_library(${PROJ_NAME} SHARED ${sources})

install(TARGETS ${PROJ_NAME} DESTINATION lib)


3.如何在C++中调用fortran编写的函数;

cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
set(PROJ_NAME PLANETOCOSMICS)
project(${PROJ_NAME})


#----------------------------------------------------------------------------
# Find Geant4 package, activating all available UI and Vis drivers by default
# You can set WITH_GEANT4_UIVIS to OFF via the command line or ccmake/cmake-gui
# to build a batch mode only executable
#
option(WITH_GEANT4_UIVIS "Build example with Geant4 UI and Vis drivers" ON)
if(WITH_GEANT4_UIVIS)
find_package(Geant4 REQUIRED ui_all vis_all)
else()
find_package(Geant4 REQUIRED)
endif()

#----------------------------------------------------------------------------
# Setup Geant4 include directories and compile definitions
# Setup include directory for this project
#
include(${Geant4_USE_FILE})
include_directories(${PROJECT_SOURCE_DIR}/include
		    ${Geant4_INCLUDE_DIR}
		    ${ROOT_INCLUDE_DIR}
			/opt/Root/root60902/include/root/
		    #/opt/Root/include/
                    #/opt/Root/include/root/
		    ${PROJECT_SOURCE_DIR}/atmosphere/
)

#----------------------------------------------------------------------------
# Find ROOT (required package)
#
find_package(ROOT REQUIRED)
#----------------------------------------------------------------------------
# Locate sources and headers for this project
# NB: headers are included so they will show up in IDEs
#
file(GLOB sources ${PROJECT_SOURCE_DIR}/src/*.cc  )
file(GLOB headers ${PROJECT_SOURCE_DIR}/include/*.hh)
file(GLOB macs  ${PROJECT_SOURCE_DIR}/root/* )
#----------------------------------------------------------------------------
# Add the executable, and link it to the Geant4 libraries
#
#add_subdirectory(magfieldmodels)

#add_custom_target(FortranEarthMagFieldlib DEPENDS fortran)

add_subdirectory(atmosphere)

add_custom_target(FortranEarthAtmospherelib DEPENDS fortran)

#add_subdirectory(lib)

#add_custom_target(spice DEPENDS fortran)

add_executable(${PROJ_NAME} ${PROJ_NAME}.cc ${sources} ${headers})

target_link_libraries(${PROJ_NAME} ${Geant4_LIBRARIES}          ${ROOT_LIBRARIES} 
		-L${PROJECT_SOURCE_DIR}/lib FortranEarthAtmosphere)

#----------------------------------------------------------------------------
# Copy all scripts to the build directory, i.e. the directory in which we
# build B1. This is so that we can run the executable directly because it
# relies on these scripts being in the current working directory.
#
set(_SCRIPTS
)

foreach(_script ${_SCRIPTS})
configure_file(

${PROJECT_SOURCE_DIR}/${_script}
${PROJECT_BINARY_DIR}/../bin/${_script}

COPYONLY
)
endforeach()

#----------------------------------------------------------------------------
# Install the executable to 'bin' directory under CMAKE_INSTALL_PREFIX
#
install(TARGETS ${PROJ_NAME} DESTINATION bin)
install(FILES ${macs} DESTINATION bin)

猜你喜欢

转载自blog.csdn.net/baidu_29950065/article/details/78740168