如何在CMake中使用libxml2? [英] How to use libxml2 with CMake?

查看:631
本文介绍了如何在CMake中使用libxml2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的C项目使用CLion编辑器(带有CMake),
虽然我从未使用过外部库,但是
我的问题是如何链接外部lib(例如libxml2) )到我的项目?
我看到了一些与此类似的问题,但没有一个对我有用。
我的项目在Windows上编译,并且我有libxml .dll,include和.lib文件(Windows二进制文件)。

I am using CLion editor (with CMake) for my C project, I've never used an external library though, my question is how do I link an external lib (for ex. libxml2) to my project? I've seen some questions similar to this but none worked for me. My project is compiled on Windows, and I have libxml .dll, include, and .lib files(binaries for Windows).

编辑:我的CMakeLists。答案后的txt文件:

My CMakeLists.txt file after the answer suggested:

cmake_minimum_required(VERSION 3.2)
project(time_table)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES
    course.c
    course.h
    day.h
    defines.h
    find_tables.c
    find_tables.h
    item.h
    parse_info.c
    parse_info.h
    table.c
    table.h
    time_table.c grades.c grades.h)

link_libraries(C:/Users/Gal/Desktop/time_table/libxml2-2.7.8.win32/lib)
add_executable(time_table ${SOURCE_FILES})
target_link_libraries(time_table libxml2)

这就是我得到的:

"C:\Program Files (x86)\JetBrains\CLion 1.1.1\bin\cmake\bin\cmake.exe" --build C:\Users\Gal\.clion11\system\cmake\generated\2eda76ff\2eda76ff\Debug --target time_table -- -j 8
[ 14%] Linking C executable time_table.exe
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot find -llibxml2
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [time_table.exe] Error 1
CMakeFiles\time_table.dir\build.make:225: recipe for target 'time_table.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/time_table.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/time_table.dir/all] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/time_table.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/time_table.dir/rule] Error 2

出什么问题了?

推荐答案

您的计算机上安装的库有一项重要工作。 < lib_name> _INCLUDE_DIR < lib_name> _LIBRARIES find_package code>。

There is a key work for libraries installed on your machine. <lib_name>_INCLUDE_DIR and <lib_name>_LIBRARIES once you have done find_package. This works for me.

find_package(LibXml2 REQUIRED)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/<my_stuff> ${LIBXML2_INCLUDE_DIR})
add_executable(<my_exe> <my_source_files>)
target_link_libraries(<my_exe> ${LIBXML2_LIBRARIES})

----只是出于您的好奇心请注意----

如果您曾经需要构建(1)计算的(静态)库(可能可重用),以及(2)使用该库和LIBXML2的可执行文件。

If you ever needed to build (1) a (static) library of your calculations, maybe reusable, and (2) an executable that uses that library and LIBXML2, do this.

find_package(LibXml2 REQUIRED)

# add a calculation library: file "lib<mycalc>.a"
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/<your header files folder> ${LIBXML2_INCLUDE_DIR})
add_library(<mycalc> STATIC ${CMAKE_CURRENT_SOURCE_DIR}/<your src files folder>)
target_link_libraries(<mycalc> ${LIBXML2_LIBRARIES}) 

# add the executable: file <my_exe>
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/<your header files folder> ${LIBXML2_INCLUDE_DIR})
add_executable(<my_exe> <your exe src files>)
target_link_libraries(<my_exe> <mycalc> ${LIBXML2_LIBRARIES}) 

这篇关于如何在CMake中使用libxml2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆