【问题标题】:How can I force CMake to look for a library in a specific directory?如何强制 CMake 在特定目录中查找库?
【发布时间】:2015-06-09 20:49:23
【问题描述】:

尝试安装 Spenvis 包,这是我在运行脚本时遇到的错误:

-- Boost version: 1.41.0
-- Found the following Boost libraries:
--   python
-- Configuring done
-- Generating done
-- Build files have been written to: Desktop/build
make[2]: *** No rule to make target `/usr/local/lib64/libpython2.6.so.1.0', needed by `source/libSpenvis.so'.  Stop.
make[1]: *** [source/CMakeFiles/Spenvis.dir/all] Error 2
make: *** [all] Error 2

它在 /usr/local/lib64 中寻找 libpython2.6.so.1.0,但库本身在 /usr/lib64 中。

我不是超级用户,所以我不能将库更改/复制到目录中,也不能创建指向它的链接。作为参考,这是我正在运行的 python 脚本,后跟 CMakeLists.txt 文件:

"""
Main launch script
"""
import os
import sys
import shutil
from subprocess import call

"""
Define path where to find python packages
"""
loc_dir=os.getcwd()
os.chdir("python_utilities")

#Spenvis csv
#############
os.chdir("spenvis_csv")
if not os.path.exists("build"):
    os.mkdir("build")

os.chdir("build")
call("cmake ../", shell=True)
call("make")
os.chdir(loc_dir)
os.chdir("python_utilities")
if not os.path.exists("lib"):
    os.mkdir("lib")

for file_name in os.listdir("spenvis_csv/build/source"):
    if file_name.find("libSpenvis.") !=-1:
        shutil.move("spenvis_csv/build/source/%s" %(file_name),"lib/Spenvis.so") 
os.chdir("..")

还有 CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)

# Make sure the compiler can find include files
include_directories (${PYSPENVIS_SOURCE_DIR})


# get boost
set(Boost_USE_STATIC_LIBS   OFF)
#set(Boost_USE_MULTIEADED ON)
find_package(Boost COMPONENTS
                python
             REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})

# get python
include(FindPythonLibs)

set(PythonLibs_USE_STATIC_LIBS   OFF)
find_package(PythonInterp)
find_package(PythonLibs)
include_directories(${PYTHON_INCLUDE_DIRS})
link_directories(${PYTHON_LIBRARIES})

add_library(Spenvis  SHARED pySpenvisCSV.cc SpenvisCSV.cc SpenvisCSVCollection.cc)
TARGET_LINK_LIBRARIES(Spenvis ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})

听取建议,我在包含 CMakeLists.txt 文件的目录中运行了以下命令:

cmake -DPYTHON_LIBRARY='/usr/lib64/libpython2.6.so.1.0'

这个结果:

Boost  found.
Found Boost components:
   python
-- Found PythonLibs: /usr/lib64/libpython2.6.so.1.0 (found version "2.7.1") 
-- Found PythonLibs: /usr/lib64/libpython2.6.so.1.0 (found suitable version "2.7.1", minimum required is "2.6") 
-- Configuring done
-- Generating done
-- Build files have been written to: blahblahblah

现在返回运行 install.py 程序返回:

-- Boost version: 1.41.0
-- Found the following Boost libraries:
--   python
-- Found PythonLibs: /usr/local/lib64/libpython2.6.so.1.0 (found version "2.6.6") 
-- Found PythonLibs: /usr/local/lib64/libpython2.6.so.1.0 (found suitable version "2.6.6", minimum required is "2.6") 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/smh/Linux/Desktop/gras original/gras-03-03/python/python_utilities/spenvis_csv/build
make[2]: *** No rule to make target `/usr/local/lib64/libpython2.6.so.1.0', needed by `source/libSpenvis.so'.  Stop.

因此,返回了相同的错误,只是现在它声称它正在查找 python 库。但奇怪的是,它找到的python库不存在,也就是说,/usr/local/lib64中没有libpython2.6.so.1.0,而是位于/usr/lib64.

在 CMakeCache.txt 中,有关所需库的行如下:

//No help, variable specified on the command line.
PYTHON_LIBRARIES:UNINITIALIZED=/usr/lib64/libpython2.6.so.1.0

//Path to a library.
PYTHON_LIBRARY:FILEPATH=/usr/lib64/libpython2.6.so

//Dependencies for the target
Spenvis_LIB_DEPENDS:STATIC=optimized;boost_python-mt-shared;debug;boost_python-mt-shared-debug;general;/usr/lib64/libpython2.6.so;

//Details about finding PythonLibs
FIND_PACKAGE_MESSAGE_DETAILS_PythonLibs:INTERNAL=[/usr/lib64/libpython2.6.so][/usr/local/include/python2.7][v2.7.1(2.6)]

【问题讨论】:

  • 您是否尝试将 /usr/lib64/libpython2.6.so.1.0 包含到 TARGET_LINK_LIBRARIES 中?
  • 在您的 make 命令后添加“VERBOSE=1”可能会有所帮助:“make VERBOSE=1”。你会看到它认为它正在使用的每个选项,即 cmake 传递的内容。
  • Damian,我该如何正确添加特定的库?
  • 您是否尝试过将/usr 添加到cmake 搜索路径中:list(APPEND CMAKE_PREFIX_PATH /usr)
  • 鱿鱼:不幸的是,这没用。

标签: python linux cmake


【解决方案1】:

/usr需要插入到CMAKE_PREFIX_PATH前面,所以会先找到。

list(INSERT 0 CMAKE_PREFIX_PATH /usr)

【讨论】:

  • 抱歉,我有一段时间被转移到另一个任务,但我现在又回到了这个。我在 CmakeLists.txt 文件的开头添加了这个,但它仍然产生了这个错误。它应该去其他地方吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-11
  • 2011-12-22
  • 2015-10-08
  • 2017-11-25
相关资源
最近更新 更多