错误LNK1104:无法打开文件'Debug \ MyProjectLib.lib'

时间:2016-11-22 10:11:26

标签: c++ qt cmake qt5

我有以下Date.prototype.addWorkDays = function (days) { if(isNaN(days)) { console.log("Value provided for \"days\" was not a number"); return } // Get the day of the week as a number (0 = Sunday, 1 = Monday, .... 6 = Saturday) var dow = this.getDay(); var daysToAdd = parseInt(days); // If the current day is Sunday add one day if (dow == 0) { daysToAdd++; } // If the start date plus the additional days falls on or after the closest Saturday calculate weekends if (dow + daysToAdd >= 6) { //Subtract days in current working week from work days var remainingWorkDays = daysToAdd - (5 - dow); //Add current working week's weekend daysToAdd += 2; if (remainingWorkDays > 5) { //Add two days for each working week by calculating how many weeks are included daysToAdd += 2 * Math.floor(remainingWorkDays / 5); //Exclude final weekend if the remainingWorkDays resolves to an exact number of weeks if (remainingWorkDays % 5 == 0) daysToAdd -= 2; } } this.setDate(this.getDate() + daysToAdd); }; //And use it like so (months are zero based) var today = new Date(2016, 10, 22) today.addWorkDays(5); // Tue Nov 29 2016 00:00:00 GMT+0000 (GMT Standard Time) 文件来生成基于Qt的项目:

CMakeLists.txt

当我尝试编译生成的项目时,我收到以下错误:

  

错误LNK1104:无法打开文件'Debug \ MyProjectLib.lib'

相应的目录cmake_minimum_required(VERSION 2.8.12) project(MyProject) find_package(Qt5Widgets) set(MyProjectLib_src ${PROJECT_SOURCE_DIR}/gui.cpp) set(MyProjectLib_hdr ${PROJECT_SOURCE_DIR}/gui.h) set(MyProjectLib_ui ${PROJECT_SOURCE_DIR}/gui.ui) set(MyProjectBin_src ${PROJECT_SOURCE_DIR}/main.cpp) qt5_wrap_cpp(MyProjectLib_hdr_moc ${MyProjectLib_hdr}) qt5_wrap_ui (MyProjectLib_ui_moc ${MyProjectLib_ui}) include_directories(${PROJECT_SOURCE_DIR}) include_directories(${PROJECT_BINARY_DIR}) add_library(MyProjectLib SHARED ${MyProjectLib_src} ${MyProjectLib_hdr_moc} ${MyProjectLib_ui_moc} ) target_link_libraries(MyProjectLib Qt5::Widgets) add_executable(MyProject ${MyProjectBin_src}) target_link_libraries(MyProject MyProjectLib) 包含:

Debug

2 个答案:

答案 0 :(得分:5)

您将MyProjectLib声明为共享库,因此除非您导出了库的全部或部分符号,否则只有.dll设计为在运行时加载,而不是{{ 1}}在编译时链接,因为你正在尝试做。

快速解决方案可能是将.lib声明为静态库:

MyProjectLib

另一种选择可能是使用" new" cmake功能可导出所有符号(请参阅this article):

add_library(MyProjectLib STATIC ...)

你也可以使用"传统"通过明确声明要导出的符号,如this answer(长答案)。您首先需要在代码中的某处声明一些API宏:

set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

请注意,cmake会自动为共享库生成#ifdef MyProjectLib_EXPORTS #define MyProjectLib_API __declspec(dllexport) #else #define MyProjectLib_API __declspec(dllimport) #endif :您不需要关心这一点。然后,对于代码中的每个类,请使用声明中的宏:

MyProjectLib_EXPORTS
编译class MyProjectLib_API MyClass { /* ... */ };

MyClass将成为导出符号,因为将定义MyProjectLib,MyProjectLib_API将扩展为MyProjectLib_EXPORTS。因此它将导出到__declspec(dllexport)文件中。

在与.lib关联时,它将是导入的符号,因为MyProjectLib未定义,MyProjectLib_EXPORTS将扩展为MyProjectLib_API

你也可以像这样改进你的cmake文件:

__declspec(dllimport)

您可以使用qt5_wrap_cpp(MyProjectLib_hdr_moc ${MyProjectLib_hdr}) qt5_wrap_ui (MyProjectLib_ui_moc ${MyProjectLib_ui}) AUTOMOC来让cmake自动处理对Qt公用程序的调用。

AUTOUIC
默认情况下,

include_directories (${PROJECT_SOURCE_DIR}) include_directories (${PROJECT_BINARY_DIR}) 是一个包含目录,我无法在此处查看您需要添加PROJECT_SOURCE_DIR的原因:只需删除这些行。

清理后,您的cmake文件可能会变成这样:

PROJECT_BINARY_DIR

答案 1 :(得分:0)

TLDR:确保您具有源文件,而不仅仅是标头!

对于到达这里但可能没有OP的问题的网络搜索者,我遇到的链接错误非常相似,因为我已经拆分了库,但其中一个没有任何源文件,即仅标头的库:

add_library(hsm STATIC
  StateAbstract.hpp        # header-only
  StateMachineAbstract.hpp # header-only
  StateMachineBase.hpp     # header-only
)

我的解决方法是在恢复类实现文件之前插入一个cpp文件:

add_library(hsm STATIC
  Placeholder.cpp # this file causes hsm.lib to get generated on Windows
  StateAbstract.hpp        # header-only
  StateMachineAbstract.hpp # header-only
  StateMachineBase.hpp     # header-only
)

这看起来似乎很明显,但是我正在该项目上进行Linux / QNX开发,而gcc创建了一个几乎为空的库:

$ wc -c build/lib/libhsm.a 
8 build/lib/libhsm.a
$ strings build/lib/libhsm.a
!<arch>

该库链接非常愉快。直到后来项目在构建服务器上构建Windows版本时,我才看到错误:

[4/6] cmd.exe /C "cd . && C:\PROGRA~2\MICROS~2\2017\COMMUN~1\VC\Tools\MSVC\1413~1.261\bin\Hostx64\x64\link.exe /lib /nologo /machine:x64 /out:build\lib\hsm.lib   && cd ."
[5/6] ...
[6/6]  cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe ... /out:build\gtest\hsm_test.exe ... build\lib\hsm.lib ...
LINK : fatal error LNK1104: cannot open file 'build\lib\hsm.lib'

由于我在构建服务器输出中看到了用于创建lib文件的命令,因此我最初无法弄清楚为什么hsm.lib不存在(并且无法进入构建服务器)。幸运的是,当我阅读其他答案并仔细检查了该链接是否为静态链接时,我注意到它只是一个标头库-哇!我感到很幸运!