0

在 qt 和 cmake 的一些 hello world 之后,这是我的折叠结构:

CMakeLists.txt
main.cpp
ui/mainwindow.ui
src/mainwindow.cpp
include/mainwindow.h

这是 CMakeList.txt:

cmake_minimum_required (VERSION 2.8)

# Project
set(PROJECTNAME "Hello")
project(${PROJECTNAME} C CXX)

set(${PROJECTNAME}_MAJOR_VERSION 0)
set(${PROJECTNAME}_MINOR_VERSION 1)

# qt
find_package(Qt4 REQUIRED)
set (QT_USE_QTMAIN TRUE)

include (${QT_USE_FILE})
add_definitions(${QT_DEFINITIONS})

# compilers
#set(CMAKE_CXX_FLAGS "-Wall -std=gnu++0x")

# files
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(src)
include_directories(include)

set(${PROJECTNAME}_SOURCES
    main.cpp
    src/mainwindow.cpp
)

set(${PROJECTNAME}_HEADERS
    include/mainwindow.h
)

set(${PROJECTNAME}_HEADERS_MOC
    include/mainwindow.h
)

set(${PROJECTNAME}_FORMS
    ui/mainwindow.ui
)

QT4_WRAP_CPP(${PROJECTNAME}_HEADERS_MOC ${${PROJECTNAME}_HEADERS_MOC})
QT4_WRAP_UI(${PROJECTNAME}_FORMS_HEADERS ${${PROJECTNAME}_FORMS})
QT4_ADD_RESOURCES(${PROJECTNAME}_RESOURCES_RCC ${${PROJECTNAME}_RESOURCES})

# executables and installation
add_executable (
    ${${PROJECTNAME}_HEADERS}
    ${${PROJECTNAME}_SOURCES}
    ${${PROJECTNAME}_HEADERS_MOC}
    ${${PROJECTNAME}_FORMS_HEADERS}
)

target_link_libraries(${PROJECTNAME} ${QT_LIBRARIES})

尝试了添加 target_link_libraries 的不同方法,但仍然得到:

-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Looking for Q_WS_X11
-- Looking for Q_WS_X11 - found
-- Looking for Q_WS_WIN
-- Looking for Q_WS_WIN - not found.
-- Looking for Q_WS_QWS
-- Looking for Q_WS_QWS - not found.
-- Looking for Q_WS_MAC
-- Looking for Q_WS_MAC - not found.
-- Found Qt4: /usr/bin/qmake (found version "4.8.1")
CMake Error at CMakeLists.txt:61 (target_link_libraries):
  Cannot specify link libraries for target "Hello" which is not built by
  this project.


-- Configuring incomplete, errors occurred!
4

1 回答 1

5

您缺少目标名称作为 add_executable 的第一个参数。

add_executable( 
  ${PROJECTNAME}     
  ${${PROJECTNAME}_HEADERS}
  ${${PROJECTNAME}_SOURCES}
  ${${PROJECTNAME}_HEADERS_MOC}
  ${${PROJECTNAME}_FORMS_HEADERS}
)
于 2013-05-29T17:03:55.970 回答