do the equivalent of a QT += modulename
Where "modulename" is what? The name of a Qt component, like XML, Widgets, or SQL? An external library?
If it is a Qt module, then you need this in your CMakelists file:
find_package(Qt6 REQUIRED COMPONENTS Widgets)
qt_standard_project_setup()
# Your custom list of sources here
add_executable(helloworld
mainwindow.ui
mainwindow.cpp
main.cpp
)
target_link_libraries(helloworld PRIVATE Qt6::Widgets)
find_package(Qt6 REQUIRED COMPONENTS Widgets)
qt_standard_project_setup()
# Your custom list of sources here
add_executable(helloworld
mainwindow.ui
mainwindow.cpp
main.cpp
)
target_link_libraries(helloworld PRIVATE Qt6::Widgets)
To copy to clipboard, switch view to plain text mode
I will typically move the declaration of target and source files into separate variables so the add_executable() command becomes generic:
# All of the customization occurs in these lines
set ( TARGET helloworld )
set ( QTVERSION ${QT_DEFAULT_MAJOR_VERSION} ) # uses an environment variable
set ( QTLIBS Widgets )
set ( SOURCES_ROOT . ) # or alternative locations if the CMakelists file is not in the same directory
set ( SOURCES
${SOURCES_ROOT}/mainwindow.ui
${SOURCES_ROOT}/mainwindow.cpp
${SOURCES_ROOT}/main.cpp
)
# So this is now boilerplate
find_package( Qt${QTVERSION} REQUIRED COMPONENTS ${QTLIBS} )
add_executable( ${TARGET} ${SOURCES} )
# Creates a list of scoped library names
foreach ( QTLIB QTLIBS )
set ( QTLINKS ${QTLINKS} Qt${QTVERSION}::${QTLIB} )
endforeach()
target_link_libraries( ${TARGET} PRIVATE ${QTLINKS} )
# All of the customization occurs in these lines
set ( TARGET helloworld )
set ( QTVERSION ${QT_DEFAULT_MAJOR_VERSION} ) # uses an environment variable
set ( QTLIBS Widgets )
set ( SOURCES_ROOT . ) # or alternative locations if the CMakelists file is not in the same directory
set ( SOURCES
${SOURCES_ROOT}/mainwindow.ui
${SOURCES_ROOT}/mainwindow.cpp
${SOURCES_ROOT}/main.cpp
)
# So this is now boilerplate
find_package( Qt${QTVERSION} REQUIRED COMPONENTS ${QTLIBS} )
add_executable( ${TARGET} ${SOURCES} )
# Creates a list of scoped library names
foreach ( QTLIB QTLIBS )
set ( QTLINKS ${QTLINKS} Qt${QTVERSION}::${QTLIB} )
endforeach()
target_link_libraries( ${TARGET} PRIVATE ${QTLINKS} )
To copy to clipboard, switch view to plain text mode
If "modulename" is an external library, simply define a variable / set of variables to specify the library names (without extensions) and modify the target_link_libraries() command.
set ( EXTRALIBS MyLib1 MyLib2 )
target_link_libraries( ${TARGET} PRIVATE ${QTLINKS} ${EXTRALIBS} )
set ( EXTRALIBS MyLib1 MyLib2 )
target_link_libraries( ${TARGET} PRIVATE ${QTLINKS} ${EXTRALIBS} )
To copy to clipboard, switch view to plain text mode
If you are building a library instead of an executable, then you replace add_executable() with add_library() and omit the linking step. You can specify whether the library is static or shared using the STATIC or SHARED keywords in the add_library() command:
set ( SHARED_FLAG STATIC )
# ...
add_library ( ${TARGET} ${SHARED_FLAG} ${SOURCES} )
set ( SHARED_FLAG STATIC )
# ...
add_library ( ${TARGET} ${SHARED_FLAG} ${SOURCES} )
To copy to clipboard, switch view to plain text mode
If I don't understand your question, please explain some more.
Note that I have defined my own variable names for QTLIBS, QTVERSION, QTLINKS, etc. There are some built-in variables that let you change the build via the command line or environment variable settings.
Bookmarks