如何使用cmake将vcpkg中的link库添加到我的库中?

how to link libraries in vcpkg to my library using cmake?

最近在尝试用cmake和vcpkg写一个vs2019的项目

我想link把vcpkg里面的动态库做成自己的动态库,但是遇到了一些麻烦

希望有人能帮我改正错误

我的项目结构:

project
-engine
 -Engine.h
 -Engine.cpp
 -Window.h
 -Window.cpp
 -InstanceManager.h
 -InstanceManager.cpp
 -MouseListener.h
 -MouseListener.cpp
 -KeyboardListener.h
 -KeyboardListener.cpp
-main.cpp
-CMakeLists.txt

CMakeLists.txt:

cmake_minimum_required (VERSION 3.8)

project ("mario")

include_directories("./engine")

find_package(GLEW REQUIRED)
find_package(glfw3 CONFIG REQUIRED)

add_executable (mario main.cpp)

add_library(engine SHARED
"engine/Engine.h" "engine/Engine.cpp" 
"engine/Window.h" "engine/Window.cpp"
"engine/InstanceManager.h" "engine/InstanceManager.cpp" 
"engine/MouseListener.h" "engine/MouseListener.cpp" 
"engine/KeyboardListener.h" "engine/KeyboardListener.cpp")

target_link_libraries(engine PRIVATE GLEW::GLEW)
target_link_libraries(engine PRIVATE glfw)

target_link_libraries(mario PRIVATE engine)

显示C1083无法打开包含文件“gl/glew.h”:没有那个文件或目录。 但是,当我写这个时:

cmake_minimum_required (VERSION 3.8)

project ("mario")

include_directories("./engine")

find_package(GLEW REQUIRED)
find_package(glfw3 CONFIG REQUIRED)

add_executable (mario main.cpp
"engine/Engine.h" "engine/Engine.cpp" 
"engine/Window.h" "engine/Window.cpp"
"engine/InstanceManager.h" "engine/InstanceManager.cpp" 
"engine/MouseListener.h" "engine/MouseListener.cpp" 
"engine/KeyboardListener.h" "engine/KeyboardListener.cpp")

target_link_libraries(mario PRIVATE GLEW::GLEW)
target_link_libraries(mario PRIVATE glfw)

它工作正常。

这是一个错误还是我犯了错误?

cmake_minimum_required (VERSION 3.8)

project ("mario")

include_directories("./engine")

set(CXX_STANDARD 11)

find_package(GLEW REQUIRED)
find_package(glfw3 CONFIG REQUIRED)

add_library(engine SHARED
"engine/Engine.h" "engine/Engine.cpp" 
"engine/Window.h" "engine/Window.cpp"
"engine/InstanceManager.h" "engine/InstanceManager.cpp" 
"engine/MouseListener.h" "engine/MouseListener.cpp" 
"engine/KeyboardListener.h" "engine/KeyboardListener.cpp")
target_link_libraries(engine PRIVATE GLEW::GLEW)
target_link_libraries(engine PRIVATE glfw)

add_executable (mario main.cpp)
target_link_libraries(mario PRIVATE GLEW::GLEW)
target_link_libraries(mario PRIVATE glfw)
target_link_libraries(mario PRIVATE engine)

可执行文件也应该 link 到 vcpkg 中的库。

target_link_libraries(engine PRIVATE GLEW::GLEW)
target_link_libraries(engine PRIVATE glfw)

错了!

应该是:

target_link_libraries(engine PUBLIC GLEW::GLEW)
target_link_libraries(engine PUBLIC glfw)