How to use a package in a library #45234
-
I'm trying to figure out how to use vcpkg in my project. When I follow it step by step, I've got no issues whatsoever. But I'm trying to do something different. main.cpp: #include "hello.h"
int main(void)
{
print_hello();
return 0;
} hello.h: #include <fmt/core.h>
void print_hello(void); hello.cpp: #include "hello.h"
void print_hello(void)
{
fmt::print("Hello, World!");
} I know that it looks a bit weird, but this is a minimal reproducible example. The project I'm working on is a bit larger, so it makes more sense there. I'm trying to make a library from hello.cpp, link external fmt library to this library, and then link this library to main.cpp. CMakeLists.txt: cmake_minimum_required(VERSION 3.10)
project(hello)
find_package(fmt CONFIG REQUIRED)
add_executable(hello main.cpp)
add_library(hellolib hello.cpp)
target_link_libraries(hellolib PRIVATE fmt::fmt)
target_link_libraries(hello PRIVATE hellolib) I know that I'm doing something wrong, but I'm not very familiar with CMake. That being said, I hope that it's possible to use vcpkg package with a library and not just with the main.cpp. When I try to build the project, this is what I get: C:\path\to\dir\hello.h(1): fatal error C1083: Cannot open include file: 'fmt/core.h': No such file or directory So it seems like CMake can't find fmt header files when I try to include them in hello.h. However, when I include them in the main.cpp (and then use them here), there are no issues. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I figured it out. I need to link fmt with hellolib and hello, and then link hellolib with hello. It can be done like so: target_link_libraries(hellolib PUBLIC fmt::fmt) |
Beta Was this translation helpful? Give feedback.
I figured it out. I need to link fmt with hellolib and hello, and then link hellolib with hello. It can be done like so:
target_link_libraries(hellolib PUBLIC fmt::fmt)
target_link_libraries(hello PRIVATE hellolib)