c++linker

LNK1181 Trying to Create Common Library


I have 3 separate C++ projects in Visual Studio.

  1. util
  2. TxtEngine
  3. TxtScreen

Lastly, I have created a project called "TxtLib2" containing the header file "txtlib2.h"

Originally the goal was to include all 3 files into the "txtLib2.h" file, with the problem being that the project couldn't be built, giving a LNK1181 error. However, I have managed to fix this by changing the project's properties. The following are the new "Additional Library Directories" for each project.

util:

C:\\Users\[name\]\\Desktop\\Projects\\Libraries\\TxtScreen\\TxtScreen;

C:\\Users\[name\]\\Desktop\\Projects\\Libraries\\TxtEngine\\TxtEngine;

C:\\Users\[name\]\\Desktop\\Projects\\Libraries\\util\\util;

%(AdditionalLibraryDirectories)

TxtEngine:

C:\\Users\[name\]\\Desktop\\Projects\\Libraries\\util\\x64\\Release;

%(AdditionalLibraryDirectories)

TxtScreen`:

C:\\Users\[name\]\\Desktop\\Projects\\Libraries\\TxtEngine\\x64\\Release;

%(AdditionalLibraryDirectories)

TxtLib2:

C:\\Users\\\[name\]\\Desktop\\Projects\\Libraries\\TxtScreen\\x64\\Release;

C:\\Users\\\[name\]\\Desktop\\Projects\\Libraries\\TxtEngine\\x64\\Release;

C:\\Users\\\[name\]\\Desktop\\Projects\\Libraries\\util\\x64\\Release;

%(AdditionalLibraryDirectories)

Below are the exact locations of each .lib file:

util.lib: C: > Users > [name] > Desktop > Projects > Libraries > util > x64 > Release > util.lib

TxtEngine.lib: C: > Users > [name] > Desktop > Projects > Libraries > TxtEngine > x64 > Release > TxtEngine.lib

TxtScreen.lib: C: > Users > [name] > Desktop > Projects > Libraries > TxtScreen > x64 > Release > TxtScreen.lib

TxtLib2.lib: C: > Users > [name] > Desktop > Projects > Libraries > TxtLib2 > x64 > Release > TxtLib2.lib

Note: all of the .lib files above are listed as "Object File Library" under "Type" in the file explorer. If this is not a valid .lib file please let me know.

Now that all projects are built, with "TxtLib2" containing the first 3 libraries, I want to use this single library as intended in a new executable project: "PicPrinter1"

Although "PicPrinter1" contains the line : #include <txtLib2.h> and this line gives no error, any classes/functions that I try to use from any of the 3 libraries included in "TxtLib2" result in an "identifier [class] is undefined" error.

Below is the "Additional Library Directories" for "PicPrinter1"

C:\\Users\\todda\\Desktop\\Projects\\Libraries\\TxtLib2\\x64\\Release;%(AdditionalLibraryDirectories)

Did I build the libraries wrong? If so, how would I fix them?


Solution

  • The problem is that you are assuming that because TxtLib2 uses code from util (say) that when PicPrinter1 uses util code you don't need to link with util because it is already included with TxtLib2. This is not true, every library that PicPrinter1 uses need to be linked with individually.

    I guess you getting confused because the TxtLib2.h file includes the util.h file, but this has nothing to do with it. Header files are about compilation not linking.

    So the solution is to add all the libraries to 'Additional Dependencies' and all the libraries directories to 'Additional Library Directories'.

    This is how static libraries work on Windows. The situation would be different with dynamic libraries (aka DLLs).