Lam Le
Lam Le

Reputation: 1839

Static library is fine alone but throws errors when being referred

I'm developing a new static library and a Win32 application inside one solution using Visual Studio 2017. The idea is simply that my Win32 project refers to the static library. However, I cannot.

The static library is fine when being built alone. But if I build Win32 application project that refers to the lib (or by setting it as Startup project), there are a lot of errors. To list a few: name followed by '::' must be a class or namespace name, 'string': is not a member of 'std', 'WRL': a namespace with this name does not exist... What I understand and guess is that the target project doesn't understand the links of the static library. (Am I wrong?)

The static library itself refers to std, Microsoft::WRL, and Microsoft::WRL::Wrappers...

I have tried to look at the ways to link static libraries (refer lib location, lib file name, header location), and even tried to enable Link Library Dependencies in Librarian > General options of the static library. No use :(

Any other directions that I can look at? Thanks in advance!

Upvotes: 1

Views: 1129

Answers (2)

Chuck Walbourn
Chuck Walbourn

Reputation: 41057

TL;DR: You need to include headers that define the types in your Win32 application. A C++ static library .lib only provides the symbols at link time, not at compile time.

C++ libraries are just collections of .obj files. You need to include headers in your application to consume the library. The .lib itself just provides the object code needed for the linker, not the type definitions for the compiler. This system dates back to the early days of C.

Typically you set up a public header for the static library that defines the types and entry-points needed to use it. Then the library itself may have it's own internal headers that it uses when building that are not intended for consumption by 'clients'.

If there is more than one public header, often they are put into a specific directory like inc or include and then the private headers and all the other source files are put elsewhere like src / source directory. This organizational trick means that clients of your library will only include the public headers without the chance of them including your private ones because they should only have your public inc / include folder in their include search paths.

In the public header(s) you typically include standard headers that are required for the header itself to compile (i.e. <vector>, <string>, etc.).

In Modern C++, best practice is to 'wrap' your types in a C++ namespace which is not that important for small projects or when you are just using static libraries to organize locally, but it becomes important when you want to share your static library for other developers to consume. There's some tricks to using C++ namespaces well, such as avoid having using namespace statements in headers, using anonymous namespaces within modules to hide private types, etc. See this tutorial for the basics.

See Walkthrough: Creating and Using a Static Library (C++)

For some examples of Visual C++ libraries that use the best practices, see DirectX Tool Kit for DX11 / DX12, DirectXTex, and DirectXMesh.

Upvotes: 1

Robert Andrzejuk
Robert Andrzejuk

Reputation: 5222

You need to change the properties of the win32 application.

The Include additional folders in Configuration properties -> C/C++ - > General have to show the location of your h files.

Upvotes: 1

Related Questions