[petsc-users] CMake error in PETSc

Jed Brown jed at jedbrown.org
Wed Dec 4 23:16:59 CST 2019


Yingjie Wu <yjwu16 at gmail.com> writes:

> Hi,
> I tried to use PKG to find the library file of PETSc, but there were some
> errors. Because I am not very familiar with cmake, I refer to different PKG
> tutorials. My CMakeList.txt is as follows. I don't know how to solve this
> error message, please give me some suggestions.
>
> yjwu at yjwu-XPS-8910:~/PETSctest/Testcmake$ cat CMakeLists.txt
> #This is a CMake makefile.
> cmake_minimum_required(VERSION 2.8.11)
>
> project(example01cmke)
>
> add_executable(example01cmke ex1.cpp)
>
> find_package(PkgConfig REQUIRED)
>
> message("petsc directories: ${PETSC_DIR}")
>
> message("petsc arch: ${PETSC_ARCH}")
>
> set(ENV{PKG_CONFIG_PATH}
> /home/yjwu/petsc-3.10.1/arch-linux2-c-debug/lib/pkgconfig)
>
> pkg_search_module(PETSC REQUIRED PETSc)
>
> message("pkg include dirs: ${PETSC_INCLUDE_DIRS}")
>
> message("pkg include libs: ${PETSC_LIBRARIES}")
>
> message("pkg ldflags: ${PETSC_LDFLAGS}")
>
> include_directories(${PETSC_INCLUDE_DIRS})
>
> target_link_libraries(example01cmke ${PETSC_LDFLAGS})
>
> yjwu at yjwu-XPS-8910:~/PETSctest/Testcmake$ cd build/
> yjwu at yjwu-XPS-8910:~/PETSctest/Testcmake/build$ cmake ..
> -- The C compiler identification is GNU 5.4.0
> -- The CXX compiler identification is GNU 5.4.0
> -- Check for working C compiler: /usr/bin/cc
> -- Check for working C compiler: /usr/bin/cc -- works
> -- Detecting C compiler ABI info
> -- Detecting C compiler ABI info - done
> -- Detecting C compile features
> -- Detecting C compile features - done
> -- Check for working CXX compiler: /usr/bin/c++
> -- Check for working CXX compiler: /usr/bin/c++ -- works
> -- Detecting CXX compiler ABI info
> -- Detecting CXX compiler ABI info - done
> -- Detecting CXX compile features
> -- Detecting CXX compile features - done
> -- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1")
> petsc directories:
> petsc arch:
> -- Checking for one of the modules 'PETSc'
> pkg include dirs:
> /home/yjwu/petsc-3.10.1/include;/home/yjwu/petsc-3.10.1/arch-linux2-c-debug/include
> pkg include libs: petsc
> pkg ldflags: -L/home/yjwu/petsc-3.10.1/lib;-lpetsc
> -- Configuring done
> -- Generating done
> -- Build files have been written to: /home/yjwu/PETSctest/Testcmake/build
> yjwu at yjwu-XPS-8910:~/PETSctest/Testcmake/build$ make
> Scanning dependencies of target example01cmke
> [ 50%] Building CXX object CMakeFiles/example01cmke.dir/ex1.cpp.o
> [100%] Linking CXX executable example01cmke
> /usr/bin/ld: cannot find -lpetsc
> collect2: error: ld returned 1 exit status

Is your PETSc fully built/installed, or just configured?

I just tested with this simplified file

cmake_minimum_required(VERSION 2.8.11)
project(example01cmke)

add_executable(example01cmke ex1.cpp)

find_package(PkgConfig REQUIRED)
pkg_search_module(PETSC REQUIRED PETSc)
message("pkg include dirs: ${PETSC_INCLUDE_DIRS}")
message("pkg include libs: ${PETSC_LIBRARIES}")
message("pkg ldflags: ${PETSC_LDFLAGS}")

include_directories(${PETSC_INCLUDE_DIRS})
target_link_libraries(example01cmke ${PETSC_LDFLAGS})


$ PKG_CONFIG_PATH=$PETSC_DIR/$PETSC_ARCH/lib/pkgconfig cmake ..
-- The C compiler identification is GNU 9.2.0
-- The CXX compiler identification is GNU 9.2.0
-- Check for working C compiler: /bin/cc
-- Check for working C compiler: /bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /bin/c++
-- Check for working CXX compiler: /bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PkgConfig: /bin/pkg-config (found version "1.6.3") 
-- Checking for one of the modules 'PETSc'
pkg include dirs: /home/jed/petsc/ompi-optg/include;/home/jed/petsc/include
pkg include libs: petsc
pkg ldflags: -L/home/jed/petsc/ompi-optg/lib;-lpetsc
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/jed/build
22:12 jedjoule12  /tmp/jed/build$ make
Scanning dependencies of target example01cmke
[ 50%] Building CXX object CMakeFiles/example01cmke.dir/ex1.cpp.o
[100%] Linking CXX executable example01cmke
[100%] Built target example01cmke


Note that you may need to set RPATH (in CMake) or export a suitable
LD_LIBRARY_PATH if your PETSc is in a non-standard location.


More information about the petsc-users mailing list