You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu环境下基于Qt Creator v6.0.1,将MyLib配置为共享库并在MyApp中调用的CMakeLists.txt修改方案咨询

Hey there! Let's walk through the exact changes you need to make to both CMakeLists.txt files to turn MyLib into a shared library and get MyApp calling it properly.

1. Modify MyLib's CMakeLists.txt (path: MyApp/libs/MyLib/CMakeLists.txt)

Replace or update the content with the following, adjusting source filenames to match your actual code:

# Minimum CMake version (v3.16+ works well with Qt 6)
cmake_minimum_required(VERSION 3.16)

# Project name and version
project(MyLib VERSION 1.0.0 LANGUAGES CXX)

# Set C++ standard to match Qt's requirements
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Add Qt modules if MyLib uses Qt (remove these lines if it's a plain C++ library)
find_package(Qt6 REQUIRED COMPONENTS Core)
qt_standard_project_setup()

# Build MyLib as a shared library (.so file on Linux)
add_library(MyLib SHARED
    src/mylib.cpp  # Replace with your actual source files from MyLib/src
    # Add other .cpp files here if needed
)

# Link Qt modules to MyLib (remove if not using Qt)
target_link_libraries(MyLib PRIVATE Qt6::Core)

# Expose MyLib's include directory to dependent projects
target_include_directories(MyLib PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/include
)

# Optional: Set shared library version properties
set_target_properties(MyLib PROPERTIES
    VERSION ${PROJECT_VERSION}
    SOVERSION 1
)

Key changes explained:

  • add_library(MyLib SHARED): Tells CMake explicitly to build MyLib as a shared library instead of the default static library.
  • target_include_directories(MyLib PUBLIC ...): Marks MyLib's include folder as public, so any project linking to MyLib automatically gets access to its header files.
  • Qt-related lines: Keep these only if MyLib uses Qt modules (like Core, Widgets). If it's a plain C++ library, you can safely remove those lines.

2. Modify MyApp's Root CMakeLists.txt (path: MyApp/CMakeLists.txt)

Update the file to include and link MyLib, adjusting for your app's actual source files:

cmake_minimum_required(VERSION 3.16)

project(MyApp VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Find Qt modules your app needs (adjust based on your app's functionality)
find_package(Qt6 REQUIRED COMPONENTS Core Widgets)
qt_standard_project_setup()

# Add MyLib as a subdirectory - this builds MyLib before MyApp
add_subdirectory(libs/MyLib)

# Create MyApp executable
add_executable(MyApp
    src/main.cpp
    src/mainwindow.cpp  # Replace with your app's source files from MyApp/src
    # Add other .cpp files here if needed
)

# Link MyApp to MyLib and Qt modules
target_link_libraries(MyApp PRIVATE
    MyLib  # Link against the shared MyLib we built
    Qt6::Core
    Qt6::Widgets
)

# Optional: Keep executable output in your existing build directory structure
set_target_properties(MyApp PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/build/$<CONFIG>
)

Key changes explained:

  • add_subdirectory(libs/MyLib): Instructs CMake to process MyLib's build first, ensuring the shared library is ready when linking MyApp.
  • target_link_libraries(MyApp PRIVATE MyLib): Connects MyApp to the MyLib shared library, so your app can call functions/classes from MyLib.
  • The RUNTIME_OUTPUT_DIRECTORY setting ensures your app's executable lands in the build/Debug or build/Release folders you already set up.

Final Quick Checks

  • Make sure all MyLib header files are placed in MyApp/libs/MyLib/include (e.g., include/mylib.h).
  • When you build MyApp in Qt Creator, CMake will first compile MyLib into libMyLib.so (in MyApp/libs/MyLib/build/<CONFIG>), then link it to MyApp automatically.

内容的提问来源于stack exchange,提问作者VRoman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 06:52:03