Error Lnk1104: Cannot Open File 'Debug\Myprojectlib.Lib'

Error LNK1104: cannot open file 'Debug\MyProjectLib.lib'

You declared MyProjectLib as a shared library, so unless you exported all or part of the symbols of the library, you will only have a .dll designed to be loaded at runtime, and no .lib to link against at compile time as you're trying to do.

A quick solution may be to declare MyProjectLib as a static library:

add_library(MyProjectLib STATIC ...)

Another option could be to use "new" cmake features to export all symbols (see this article):

set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

You can also use the "traditional" way by explicitly declaring the symbols to be exported, like in this answer (the long answer). You will first need to declare some API macro somewhere in your code:

#ifdef MyProjectLib_EXPORTS
#define MyProjectLib_API __declspec(dllexport)
#else
#define MyProjectLib_API __declspec(dllimport)
#endif

Note that MyProjectLib_EXPORTS is automatically generated by cmake for shared libraries: you don't need to care about this. Then for each of your class in your code, use the macro in the declaration:

class MyProjectLib_API MyClass { /* ... */ };

MyClass will be an exported symbol when compiling MyProjectLib because MyProjectLib_EXPORTS will be defined, and MyProjectLib_API will expand to __declspec(dllexport). So it will be exported in a .lib file.

It will be an imported symbol when linking against MyProjectLib because MyProjectLib_EXPORTS will be undefined, and MyProjectLib_API will expand to __declspec(dllimport).


You may also improve your cmake file like this:

qt5_wrap_cpp(MyProjectLib_hdr_moc ${MyProjectLib_hdr})
qt5_wrap_ui (MyProjectLib_ui_moc ${MyProjectLib_ui})

You may use AUTOMOC and AUTOUIC instead to let cmake automatically handle the call to Qt's utilities.

include_directories (${PROJECT_SOURCE_DIR})
include_directories (${PROJECT_BINARY_DIR})

PROJECT_SOURCE_DIR is an include directory by default, and I can't see why you need to add PROJECT_BINARY_DIR here: just remove these lines.

Once cleaned, your cmake file may become something like this:

cmake_minimum_required(VERSION 2.8.12)
project(MyProject)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)

find_package(Qt5Widgets)

set(MyProjectLib_src
${PROJECT_SOURCE_DIR}/gui.cpp
${PROJECT_SOURCE_DIR}/gui.h
${PROJECT_SOURCE_DIR}/gui.ui
)

add_library(MyProjectLib STATIC
${MyProjectLib_src}
)
target_link_libraries(MyProjectLib Qt5::Widgets)

set(MyProjectBin_src ${PROJECT_SOURCE_DIR}/main.cpp)

add_executable(MyProject
${MyProjectBin_src}
)
target_link_libraries (MyProject MyProjectLib)

getting LNK1104 error from a referenced project's .lib file

You didn't set the Additional Include Directories setting correctly. Your .h file surely isn't present in the Debug folder. Make it U:\Software Development\c++ projects\myProject instead.

Do make sure the .lib file actually exists after building the "myProject" project. If it is missing then you forgot to use __declspec(dllexport) to tell the linker to export the functions or classes that you want to make available.

And make sure that the projects are getting build in the correct order, "myProject" must be built before the console project starts building. If necessary, right-click the console project in the Solution Explorer window, click "Project Dependencies" and tick myProject in the dialog box.



Related Topics



Leave a reply



Submit