0

I have a question about using CMake to link the shared library.

After building OpenBLAS from source and install it succesfully. In /opt/OpenBLAS/include has header files and in /opt/OpenBLAS/lib has shared and static libraries.

If I have a toy program call cpp_gemm.cpp, I can build it by typing

g++ -o cpp_gemm cpp_gemm.cpp -I/opt/OpenBLAS/include -L/opt/OpenBLAS/lib -lopenblas

And execute it without error message.

But I want to use CMake to build it, so I write following rules in CMakeLists.txt

cmake_minimum_required(VERSION 2.8.4)
project(CppGEMM)

include_directories(/opt/OpenBLAS/include)
link_directories(/opt/OpenBLAS/lib)
add_executable(cpp_gemm cpp_gemm.cpp)
target_link_libraries(cpp_gemm openblas)

After I create build folder and enter into it, I type

cmake ..
make

to build the program successfully. But I will get

Illegal instruction (core dumped)

after execute it.

After searching the related problems by Google search, I found that replace

link_directories(/opt/OpenBLAS/lib)

with

link_directories(/opt/OpenBLAS)

can solve my problem. That is, I can build the program successfully and execute it without the error message.

I'm confused, but I can not find any material to realize it. Can anyone tell me why this occurred? Many thanks!

Nestarneal
  • 205
  • 1
  • 2
  • 12
  • 2
    1) Use something newer than CMake 2.8 2) Don't use absolute paths in your CMake files (use the `find_library` command or _Find_ modules) 3) Don't use `link_directories` (use imported targets) – Torbjörn Aug 26 '16 at 10:25
  • Hello Torbjörn, thanks for you response. But the problem still exists. I change `link_directories(/opt/OpenBLAS/lib)` to `find_library(OPENBLAS_LIB NAMES openblas HINTS /opt/OpenBLAS)` and `target_link_libraries(cpp_gemm openblas)` to `target_link_libraries(cpp_gemm ${OPENBLAS_LIB})` can build and execute it successfully. But the path `/opt/OpenBLAS/lib` still cannot work except for specifying the path to `/opt/OpenBLAS`. I've tried the latest CMake release 3.6.1, but the error `Illegal instruction (core dumped)` still occurred. – Nestarneal Aug 29 '16 at 07:26
  • Try to debug the CMake generated compiler invocations with the help of `CMAKE_VERBOSE_MAKEFILE`. – Torbjörn Aug 29 '16 at 10:15
  • Is it possible that there are two libraries with the same name on your computer? Possibly one contains an instruction set that is not supported on your platform. I would figure the version in /opt/OpenBLAS/lib has the illegal instruction. Try ldd on the executable under both configurations and see if there is a difference. – Robert Prévost Aug 31 '16 at 03:03
  • You are right. I have another libopenblas.so in /usr/lib, and when I execute the executable, it use the library in /usr/lib instead of the one in /opt/OpenBLAS. I use rpath to make the executable use the shared library in /opt/OpenBLAS to solve the problem. Thank you very much! – Nestarneal Sep 08 '16 at 06:12

0 Answers0