将编译库添加到CMake Qt Project?

时间:2012-08-03 13:48:24

标签: c++ visual-studio-2010 qt cmake

我一直在尝试将最新版本的qextserialport包含到我正在处理的项目中。项目结构如下:

  • CameraProject
    • CameraProject.pro
    • CameraProject
      • 接头
      • 来源
    • qextserialport
      • 接头
      • 来源

我的CMake文件目前是这样的:

PROJECT(CameraProject)
FIND_PACKAGE(Qt4 REQUIRED)

FIND_LIBRARY(SIMPLONLIB lv.simplon lib)
FIND_LIBRARY(SIMPLONIMGPROC lv.simplon.imgproc lib)

SET(CameraProject_SOURCES include/lv.simplon.class.cpp camera.cpp main.cpp mainwindow.cpp osdep.cpp paint.cpp)
SET(CameraProject_HEADERS include/lv.simplon.class.h camera.h mainwindow.h osdep.h paint.h)
SET(CameraProject_RESOURCES icons.qrc)

INCLUDE(${QT_USE_FILE})
ADD_DEFINITIONS(${QT_DEFINITIONS})

ADD_EXECUTABLE(CameraProject ${CameraProject_SOURCES}
                ${CameraProject_HEADERS_MOC}
                ${CameraProject_RESOURCES_RCC})

TARGET_LINK_LIBRARIES(CameraProject ${QT_LIBRARIES} ${SIMPLONLIB} ${SIMPLONIMGPROC})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR} include QExtSerialPort/src)

尽管在Include目录中包含了QextSerialPort文件,但我仍然会绘制链接器问题。

编辑:更多信息!

这里找到的是像CameraProject.pro中包含的qextserialport.pri文件,如下所示:include(./QExtSerialPort/src/qextserialport.pri)

我是否应该为源文件夹中放置的qextserialport库制作第二个CMakeLists文件?

非常感谢任何意见,请不要犹豫要求澄清或任何进一步的信息。

1 个答案:

答案 0 :(得分:2)

对qxt的作用是使用cmake finder模块而不是直接将代码包含到我的项目中。然后我用qmake构建了qxt,让CMake找到它创建的库。

#############
## basic FindQxt.cmake
## This is an *EXTREMELY BASIC* cmake find/config file for
## those times you have a cmake project and wish to use
## libQxt.
##
## It should be noted that at the time of writing, that
## I (mschnee) have an extremely limited understanding of the
## way Find*.cmake files work, but I have attempted to
## emulate what FindQt4.cmake and a few others do.
##
##  To enable a specific component, set your QXT_USE_${modname}:
##  SET(QXT_USE_QXTCORE TRUE)
##  SET(QXT_USE_QXTGUI FALSE)
##  Currently available components:
##  QxtCore, QxtGui, QxtNetwork, QxtWeb, QxtSql
##  Auto-including directories are enabled with INCLUDE_DIRECTORIES(), but
##  can be accessed if necessary via ${QXT_INCLUDE_DIRS}
##
## To add the libraries to your build, TARGET_LINK_LIBRARIES(), such as...
##  TARGET_LINK_LIBRARIES(YourTargetNameHere ${QXT_LIBRARIES})
## ...or..
##  TARGET_LINK_LIBRARIES(YourTargetNameHere ${QT_LIBRARIES} ${QXT_LIBRARIES})
################### TODO:
##      The purpose of this cmake file is to find what components
##  exist, regardless of how libQxt was build or configured, thus
##  it should search/find all possible options.  As I am not aware
##  that any module requires anything special to be used, adding all
##  modules to ${QXT_MODULES} below should be sufficient.
##      Eventually, there should be version numbers, but
##  I am still too unfamiliar with cmake to determine how to do
##  version checks and comparisons.
##      At the moment, this cmake returns a failure if you
##  try to use a component that doesn't exist.  I don't know how to
##  set up warnings.
##      It would be nice having a FindQxt.cmake and a UseQxt.cmake
##  file like done for Qt - one to check for everything in advance

##############

###### setup
SET(QXT_MODULES QxtGui QxtWeb QxtZeroConf QxtNetwork QxtSql QxtBerkeley QxtCore)
SET(QXT_FOUND_MODULES)
FOREACH(mod ${QXT_MODULES})
    STRING(TOUPPER ${mod} U_MOD)
    SET(QXT_${U_MOD}_INCLUDE_DIR NOTFOUND)
    SET(QXT_${U_MOD}_LIB_DEBUG NOTFOUND)
    SET(QXT_${U_MOD}_LIB_RELEASE NOTFOUND)
    SET(QXT_FOUND_${U_MOD} FALSE)
ENDFOREACH(mod)
SET(QXT_QXTGUI_DEPENDSON QxtCore)
SET(QXT_QXTWEB_DEPENDSON QxtCore QxtNetwork)
SET(QXT_QXTZEROCONF_DEPENDSON QxtCore QxtNetwork)
SET(QXT_QXTNETWORK_DEPENDSON QxtCore)
SET(QXT_QXTQSQL_DEPENDSON QxtCore)
SET(QXT_QXTBERKELEY_DEPENDSON QxtCore)

FIND_PATH(QXT_DIR libqxt.pro Qxt/include/QxtCore/Qxt)
FIND_PATH(QXT_BINARY_DIR 
    NAMES QxtCore.dll QxtCored.dll 
    PATHS 
    ${QXT_DIR}/bin  
    ${QXT_DIR}/Bin 
    NO_DEFAULT_PATH
)

#SET(QXT_BINARY_DIR "${QXT_DIR}/bin" CACHE PATH "${QXT_DIR}/bin")

FOREACH(mod ${QXT_MODULES})
    STRING(TOUPPER ${mod} U_MOD)
    FIND_PATH(QXT_${U_MOD}_INCLUDE_DIR ${mod}
        PATH_SUFFIXES ${mod} include/${mod} Qxt/include/${mod} include/Qxt/${mod}
        PATHS
        ~/Library/Frameworks/
        /Library/Frameworks/
        /sw/
        /usr/local/
        /usr
        /opt/local/
        /opt/csw
        /opt
        "C:\\"
        "C:\\Program Files\\"
        "C:\\Program Files(x86)\\"
        ${QXT_DIR}
        NO_DEFAULT_PATH
    )
    FIND_LIBRARY(QXT_${U_MOD}_LIB_RELEASE NAME ${mod}
        PATH_SUFFIXES Qxt/lib64 Qxt/lib lib64 lib
        PATHS
        /sw
        /usr/local
        /usr
        /opt/local
        /opt/csw
        /opt
        "C:\\"
        "C:\\Program Files"
        "C:\\Program Files(x86)"
        ${QXT_DIR}
        NO_DEFAULT_PATH
    )
    FIND_LIBRARY(QXT_${U_MOD}_LIB_DEBUG NAME ${mod}d
        PATH_SUFFIXES Qxt/lib64 Qxt/lib lib64 lib
        PATHS
        /sw
        /usr/local
        /usr
        /opt/local
        /opt/csw
        /opt
        "C:\\"
        "C:\\Program Files"
        "C:\\Program Files(x86)"
        ${QXT_DIR}
        NO_DEFAULT_PATH
    )
    IF (QXT_${U_MOD}_LIB_RELEASE)
        SET(QXT_FOUND_MODULES "${QXT_FOUND_MODULES} ${mod}")
    ENDIF (QXT_${U_MOD}_LIB_RELEASE)

    IF (QXT_${U_MOD}_LIB_DEBUG)
        SET(QXT_FOUND_MODULES "${QXT_FOUND_MODULES} ${mod}")
    ENDIF (QXT_${U_MOD}_LIB_DEBUG)
ENDFOREACH(mod)

FOREACH(mod ${QXT_MODULES})
    STRING(TOUPPER ${mod} U_MOD)
    IF(QXT_${U_MOD}_INCLUDE_DIR AND QXT_${U_MOD}_LIB_RELEASE)
        SET(QXT_FOUND_${U_MOD} TRUE)
    ENDIF(QXT_${U_MOD}_INCLUDE_DIR AND QXT_${U_MOD}_LIB_RELEASE)
ENDFOREACH(mod)


##### find and include
# To use a Qxt Library....
#   SET(QXT_FIND_COMPONENTS QxtCore, QxtGui)
# ...and this will do the rest
IF( QXT_FIND_COMPONENTS )
    FOREACH( component ${QXT_FIND_COMPONENTS} )
        STRING( TOUPPER ${component} _COMPONENT )
        SET(QXT_USE_${_COMPONENT}_COMPONENT TRUE)
    ENDFOREACH( component )
ENDIF( QXT_FIND_COMPONENTS )

SET(QXT_LIBRARIES "")
SET(QXT_INCLUDE_DIRS "")

# like FindQt4.cmake, in order of dependence
FOREACH( module ${QXT_MODULES} )
    STRING(TOUPPER ${module} U_MOD)
    IF(QXT_USE_${U_MOD} OR QXT_DEPENDS_${U_MOD})
        IF(QXT_FOUND_${U_MOD})
            STRING(REPLACE "QXT" "" qxt_module_def "${U_MOD}")
            ADD_DEFINITIONS(-DQXT_${qxt_module_def}_LIB)
            SET(QXT_INCLUDE_DIRS ${QXT_INCLUDE_DIRS} ${QXT_${U_MOD}_INCLUDE_DIR})
            INCLUDE_DIRECTORIES(${QXT_${U_MOD}_INCLUDE_DIR})
            SET(QXT_LIBRARIES ${QXT_LIBRARIES};optimized;${QXT_${U_MOD}_LIB_RELEASE};debug;${QXT_${U_MOD}_LIB_DEBUG})
        ELSE(QXT_FOUND_${U_MOD})
            MESSAGE("Could not find Qxt Module ${module}")
            RETURN()
        ENDIF(QXT_FOUND_${U_MOD})
        FOREACH(dep QXT_${U_MOD}_DEPENDSON)
            SET(QXT_DEPENDS_${dep} TRUE)
        ENDFOREACH(dep)
    ENDIF(QXT_USE_${U_MOD} OR QXT_DEPENDS_${U_MOD})
ENDFOREACH(module)
MESSAGE(STATUS "Found Qxt Libraries:${QXT_FOUND_MODULES}")

MESSAGE(STATUS "Include directories:${QXT_INCLUDE_DIRS}")

我将此模块放在我的根项目的一个名为CMake / Modules的子目录中,并使用以下命令将其包含在根CMakeLists.txt顶部附近的cmake中:


    # The following line will add additional finders to CMake without the need to be placed in the CMake install path
    LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMake/Modules)

最后让CMake找到Qxt

SET(QXT_FIND_COMPONENTS QxtCore, QxtGui)
SET(QXT_USE_QXTCORE TRUE)
FIND_PACKAGE(Qxt REQUIRED)