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

Pierre Jolivet pierre at joliv.et
Thu Dec 14 00:45:38 CST 2023



> On 14 Dec 2023, at 4:13 AM, 2111191--- via petsc-users <petsc-users at mcs.anl.gov> wrote:
> 
> 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)).

This compiler (gcc 4.8.5) is known to not be C++11 compliant, but you are using the -std=c++11 flag.
Furthermore, since version 3.18 (or maybe slightly later), PETSc requires a C++11-compliant compiler if using C++.
Could you switch to a newer compiler, or try to reconfigure?
Also, you should not put all the include inside an extern { }.
In any case, you’ll need to send the compilation error log and configure.log to petsc-maint at mcs.anl.gov <mailto:petsc-maint at mcs.anl.gov> if you want further help, as we won’t be able to give a better diagnosis with just the currently provided information.

Thanks,
Pierre

> 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20231214/09b25541/attachment-0001.html>


More information about the petsc-users mailing list