修改CMake文件以添加libpng

时间:2014-05-07 05:52:31

标签: cmake

我已下载CmakeLists.txt附带的libviso2库。 要使用visual studio 11编译器构建libviso2,需要在CmakeLists.txt中将libpng添加为依赖项。

现在我的问题是:

如何修改CMakeLists.txt以在构建项目之前添加libpng?

CMakeLists.txt是:

# project
cmake_minimum_required (VERSION 2.6)
project (libviso2)

# directories
set (LIBVISO2_SRC_DIR src)

# include directory
include_directories("${LIBVISO2_SRC_DIR}")

# use sse3 instruction set
SET(CMAKE_CXX_FLAGS "-msse3")

# sources
FILE(GLOB LIBVISO2_SRC_FILES "src/*.cpp")

# make release version
set(CMAKE_BUILD_TYPE Release)

# demo program
add_executable(viso2 ${LIBVISO2_SRC_FILES})
target_link_libraries (viso2 png)

1 个答案:

答案 0 :(得分:0)

如果在drive:\path\to\libpng安装了libpng,安装libpng的目录应该包含以下文件夹:binincludelib。根据您拥有的版本,drive:\path\to\libpng\lib中的文件应命名为libpngXX.lib,其中XX是您的库的版本。

所以你的libviso2 CMakeLists应该看起来像

# project
cmake_minimum_required (VERSION 2.6)
project (libviso2)

# directories
set (LIBVISO2_SRC_DIR src)

# include directory
include_directories("${LIBVISO2_SRC_DIR} drive:/path/to/libpng/include")

# link directories
link_directories("drive:/path/to/libpng/lib")

# use sse3 instruction set
SET(CMAKE_CXX_FLAGS "-msse3")

# sources
FILE(GLOB LIBVISO2_SRC_FILES "src/*.cpp")

# make release version
set(CMAKE_BUILD_TYPE Release)

# demo program
add_executable(viso2 ${LIBVISO2_SRC_FILES})
target_link_libraries (viso2 libpngXX)

使用CMake gui,您应该能够生成Visual Studio项目并编译viso2.exe可执行文件。请注意,libviso2中的CMakeLists文件只构建viso2可执行文件,而不是库。

要运行viso2.exe程序,您需要将drive:\path\to\libpng\bin添加到Path环境变量中。