[petsc-users] Some question about compiling c++ program including PETSc using cmake

2111191 at tongji.edu.cn 2111191 at tongji.edu.cn
Wed Dec 13 21:13:01 CST 2023


Dear SLEPc Developers,

I a am student from Tongji University. Recently I am trying to write a c++ program for matrix solving, which requires importing the PETSc library that you have developed. However a lot of errors occur in the cpp file when I use #include <petscts.h> directly. I also try to use extern "C" but it gives me the error in the picture below. Is there a good way to use the PETSc library in a c++ program? (I compiled using cmake and my compiler is g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)).

My cmakelists.txt is:

cmake_minimum_required(VERSION 3.1.0)

set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

set(PETSC $ENV{PETSC_DIR}/$ENV{PETSC_ARCH})
set(SLEPC $ENV{SLEPC_DIR}/$ENV{PETSC_ARCH})
set(ENV{PKG_CONFIG_PATH} ${PETSC}/lib/pkgconfig:${SLEPC}/lib/pkgconfig)

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")  
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")  

project(test)

add_executable(${PROJECT_NAME} eigen_test2.cpp)
find_package(PkgConfig REQUIRED)

pkg_search_module(PETSc REQUIRED IMPORTED_TARGET PETSc)
target_link_libraries(${PROJECT_NAME} PkgConfig::PETSc)

The testing code is:eigen_test2.cpp
extern "C"{
   //#include <petsc.h>
   #include <petscts.h>
   #include <petscdm.h>
   #include <petscdmda.h>
   #include <petscdraw.h>
}

int main(int argc,char **argv)
{ 
   return 0;
}



Best regards

Weijie Xu


More information about the petsc-users mailing list