Visual Studio 2017 不链接库



我正在尝试在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:UsersusersourcereposCMakeProject1buildDebugCMakeProject1.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没有链接到库。有人知道怎么修吗?

展开@fabian的评论,你应该

set(SPDLOG_WCHAR_TO_UTF8_SUPPORT ON)

add_subdirectory调用之前让CMake为你设置预处理器符号。

最新更新