dftlibs / xcfun

XCFun: A library of exchange-correlation functionals with arbitrary-order derivatives
https://dftlibs.org/xcfun/
Mozilla Public License 2.0
57 stars 32 forks source link

Document how to build and link with CMake #60

Closed robertodr closed 4 years ago

robertodr commented 6 years ago

Pending #59, this will be done like this:

cmake -H. -Bbuild -DXCFun_DIR=<install_prefix>/share/cmake/XCFun

and CMake will find XCFun on the system.

cmake_minimum_required(VERSION 3.6 FATAL_ERROR)

project(UseXCFun LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(GNUInstallDirs)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})

find_package(XCFun CONFIG REQUIRED)
get_property(_loc TARGET XCFun::xcfun PROPERTY LOCATION)
message(STATUS "Found XCFun: ${_loc} (found version ${XCFun_VERSION})")

add_executable(use_xcfun use_xcfun.cpp)
target_link_libraries(use_xcfun
  PUBLIC
  XCFun::xcfun
  )

set_target_properties(use_xcfun
  PROPERTIES
    MACOSX_RPATH ON
    SKIP_BUILD_RPATH OFF
    BUILD_WITH_INSTALL_RPATH OFF
    INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}"
    INSTALL_RPATH_USE_LINK_PATH ON
  )

with the source file use_xcfun.cpp:

#include <cstdlib>
#include <iostream>

#ifdef USING_XCFun
#include "XCFun/xcfun.h"
#endif

int main() {
#ifdef USING_XCFun
  std::cout << "We have XCFun" << std::endl;
  std::cout << xcfun_splash() << std::endl;
#endif
  return EXIT_SUCCESS;
}

I assume the same works for Fortran, but I haven't tried.

robertodr commented 6 years ago
robertodr commented 4 years ago

Done with #98 Will continue in #106