Visual Studio 2017 不链接库
Posted
技术标签:
【中文标题】Visual Studio 2017 不链接库【英文标题】:Visual Studio 2017 doesn't link the library 【发布时间】:2021-09-02 05:53:35 【问题描述】:我正在尝试在 Visual Studio 2017 中构建一个简单的 C++ 程序。我在 Linux 上执行此操作时没有遇到任何问题,但由于某种原因它无法在 Windows 上运行。
这是程序:
// CMakeProject1.cpp : Defines the entry point for the application.
//
#include "CMakeProject1.h"
#include "fmt/core.h"
#include "fmt/xchar.h"
#include "spdlog/sinks/win_eventlog_sink.h"
#include "spdlog/sinks/msvc_sink.h"
#include "spdlog/sinks/wincolor_sink.h"
#include "spdlog/spdlog.h"
#include <exception>
#include <vector>
int main()
std::vector<spdlog::sink_ptr> sinks;
sinks.push_back(std::make_shared<spdlog::sinks::win_eventlog_sink_mt>("me"));
sinks.push_back(std::make_shared<spdlog::sinks::msvc_sink_mt>());
sinks.push_back(std::make_shared<spdlog::sinks::wincolor_stdout_sink_mt>());
auto logger = std::make_shared<spdlog::logger>("main-logger", begin(sinks), end(sinks));
SPDLOG_LOGGER_CALL(logger, spdlog::level::info, L"", L"Hello World!");
return 0;
这是我的 CMakeLists.txt
# CMakeList.txt : Top-level CMake project file, do global configuration
# and include sub-projects here.
#
cmake_minimum_required(VERSION 3.20)
get_filename_component(Project $CMAKE_CURRENT_SOURCE_DIR NAME)
string(REPLACE " " "-" Project $Project)
project($Project CXX C)
set(CMAKE_VERBOSE_MAKEFILE ON)
include(FetchContent)
FetchContent_Declare(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 8.0.1
)
FetchContent_MakeAvailable(fmt)
if (NOT fmt_POPULATED)
FetchContent_Populate(fmt)
add_subdirectory($fmt_SOURCE_DIR $fmt_BINARY_DIR)
endif()
message(STATUS "fmt_SOURCE_DIR = $fmt_SOURCE_DIR")
message(STATUS "fmt_BINARY_DIR = $fmt_BINARY_DIR")
FetchContent_Declare(
spdlog
GIT_REPOSITORY https://github.com/gabime/spdlog.git
GIT_TAG v1.9.2
)
FetchContent_MakeAvailable(spdlog)
if (NOT spdlog_POPULATED)
FetchContent_Populate(spdlog)
set(SPDLOG_BUILD_EXAMPLES OFF)
set(SPDLOG_BUILD_BENCH OFF)
set(SPDLOG_BUILD_TESTS OFF)
add_subdirectory($spdlog_SOURCE_DIR $spdlog_BINARY_DIR)
endif()
message(STATUS "spdlog_SOURCE_DIR = $spdlog_SOURCE_DIR")
message(STATUS "spdlog_BINARY_DIR = $spdlog_BINARY_DIR")
set(SourceDir $CMAKE_CURRENT_SOURCE_DIR)
set(ExecutableSources $SourceDir/main.cpp)
set(LinkLibraries fmt::fmt spdlog::spdlog)
set(Executable $Project)
message(STATUS CMAKE_VERSION = $CMAKE_VERSION)
message(STATUS LinkLibraries = $LinkLibraries)
add_executable($Executable $ExecutableSources)
add_definitions(-DSPDLOG_WCHAR_TO_UTF8_SUPPORT)
target_compile_options($Executable PUBLIC $CompilationFlags)
target_link_libraries($Executable PUBLIC $LinkLibraries)
生成构建文件的方式与在 Linux 上的方式相同
mkdir build
cd build
cmake ..
然后我在 Visual Studio 2017 中打开解决方案并构建它。但是我收到以下错误:
4>main.cpp
4>main.obj : error LNK2019: unresolved external symbol "void __cdecl spdlog::details::os::wstr_to_utf8buf(class fmt::v8::basic_string_view<wchar_t>,class fmt::v8::basic_memory_buffer<char,250,class std::allocator<char> > &)" (?wstr_to_utf8buf@os@details@spdlog@@YAXV?$basic_string_view@_W@v8@fmt@@AAV?$basic_memory_buffer@D$0PK@V?$allocator@D@std@@@56@@Z) referenced in function "protected: void __thiscall spdlog::logger::log_<wchar_t const (&)[13]>(struct spdlog::source_loc,enum spdlog::level::level_enum,class fmt::v8::basic_string_view<wchar_t>,wchar_t const (&)[13])" (??$log_@AAY0N@$$CB_W@logger@spdlog@@IAEXUsource_loc@1@W4level_enum@level@1@V?$basic_string_view@_W@v8@fmt@@AAY0N@$$CB_W@Z)
4>main.obj : error LNK2019: unresolved external symbol "void __cdecl spdlog::details::os::utf8_to_wstrbuf(class fmt::v8::basic_string_view<char>,class fmt::v8::basic_memory_buffer<wchar_t,250,class std::allocator<wchar_t> > &)" (?utf8_to_wstrbuf@os@details@spdlog@@YAXV?$basic_string_view@D@v8@fmt@@AAV?$basic_memory_buffer@_W$0PK@V?$allocator@_W@std@@@56@@Z) referenced in function "protected: virtual void __thiscall spdlog::sinks::win_eventlog::win_eventlog_sink<class std::mutex>::sink_it_(struct spdlog::details::log_msg const &)" (?sink_it_@?$win_eventlog_sink@Vmutex@std@@@win_eventlog@sinks@spdlog@@MAEXABUlog_msg@details@4@@Z)
4>C:\Users\user\source\repos\CMakeProject1\build\Debug\CMakeProject1.exe : fatal error LNK1120: 2 unresolved externals
4>Done building project "CMakeProject1.vcxproj" -- FAILED.
========== Rebuild All: 3 succeeded, 1 failed, 0 skipped ==========
看起来 Visual Studio 2017 没有链接到库。有人知道怎么解决吗?
【问题讨论】:
不熟悉该库,但我假设此错误消息是spdlog
库未在 Windows 上使用正确的选项编译,从而阻止编译您可以通过定义激活使用的可选功能预处理器符号SPDLOG_WCHAR_TO_UTF8_SUPPORT
【参考方案1】:
扩展@f*** 的评论,你应该
set(SPDLOG_WCHAR_TO_UTF8_SUPPORT ON)
在add_subdirectory
调用have CMake set that preprocessor symbol for you 之前。
【讨论】:
以上是关于Visual Studio 2017 不链接库的主要内容,如果未能解决你的问题,请参考以下文章
Visual Studio 2017 动态链接库(DLL /LIB) 静态链接库(LIB)的思考