How to Add Static Libraries to a Visual Studio Project

How to add static libraries to a Visual studio project

The tutorial you have provided refers to a case in which you create your own static library - in this case you may want to add it to your solution and thus make it an integral part of the solution; but I assume you are trying to add external libraries (not created by you, e.g. downloaded from the net) - that is why you got stuck.

On Property Pages, go to C/C++->General->Additional Include Directories and provide the path, where the header file of the library that you want to use is located.

Then go to Linker->General->Additional Library Directories and specify the path, where your .lib file is located.

Finally, go to Linker->Input->Additional Dependencies and add the name of the file containing your library together with its extension (e.g. example_library.lib).

That is all. Now you should be able to use the library. Remember to #include it in your files and use the right mode (release/debug) and the right version for your platform (x64/win32). You may have to repeat the steps given above both for release and debug versions of your app.

Linking a static library to a new project in Visual C++

When you #include a .h file, you are usually just including declarations into your code (exceptions apply, but not in this case). These are separate from definitions which are placed into .cpp files.

By default your projects do not automatically shared any of this information with each other. Your definitions nor declarations from Project A do not magically propagate to Project B, for example.

But as you've done here, you can reference declarations from Project A into Project B by #includeing header files. These, however, are just declarations! They just explain the existence of a function but don't implement it -- You need to add the definition of your Add function to Project B.

The process of bringing such definitions together is called linking. You need to link the definitions of Project A (the .lib, more precisely) into Project B. The way you do this changes with each version of Visual Studio just slightly, but it's usually under some "Linker Options" and some "Dependencies" tab/window.

How to link a static library in Visual C++ 2017?

In the Solution Explorer, right click on the project, select Properties. Expand to Configuration Properties > Linker > Input. Add the .lib file to Additional Dependencies. Do this for both the Release and Debug configuration.

How do I get a static library project talking to my main C++ project in the same solution?

In Solution Explorer, your main project should have a References item. Right-click it and Add Reference, then check the static library project. Note this is better than using Project, Properties, Linker to add additional library paths because it also sets up the correct build order for the projects.

To located the header, you can either provide the relative path to the header from your main project:

#include "../staticlib/header.h"

Or, on your main project, go to Project, Properties, C++, Additional Include Directories and add the relative path from your main project to the static library header, e.g. ../staticlib.

Both examples are based on a file structure like the following:

MYPROJECT
│ MyProject.sln

├───MainExe
│ MainExe.vcxproj

└───StaticLib
Header.h
StaticLib.vcxproj

Visual Studio: project reference vs link static lib

In my opinion, when a project needs to use a library, you have to do at least two things:

  1. Include header files corresponding to the library
  2. When linking, add the corresponding .lib in VS

File When there are multiple projects in your solution, for example, Project A is exe and Project B is static lib. If Project A depends on Project B, you need to do three things:

  1. Include B's header file in A project
  2. Add b.lib to the link option of Project A, and pay attention to the difference between debug/relase
  3. Whenever B is updated, you need to compile B first and then recompile A.

Since the above actions are standard actions, VS provides a function called Add Reference, which automatically completes these two actions:

  1. When linking A, automatically bring b.lib, debug/release can automatically distinguish
  2. When project B changes, if project B is compiled before project A is compiled

If you want to konw more information, you could refer to Microsoft Docs about Create and use a static library and Manage references in a project.

How can i add a static library of type .a to a visual studio project?

According with the documentation of MathGL, the libraries are into the directory mathgl/lib, and they are dinamic link libraries(dlls):

Sample Image

The same documentation says:

Use a precompiled binary. There are binaries for MinGW (platform Win32). For
a precompiled variant one needs only to unpack the archive to the location of the
Chapter 1: Overview 3
compiler (i.e. mathgl/lib in mingw/lib, mathgl/include in mingw/include and so on)
or in arbitrary other folder and setup paths in compiler. By default, precompiled
versions include the support of GSL (www.gsl.org) and PNG. So, one needs to have
these libraries installed on system (it can be found, for example, at http://gnuwin32.
sourceforge.net/packages.html).

So, you need to link the Dll instead of a static library, check this asnwer to see how to link a dll: Linking dll in Visual Studio

Create a static library .lib for sources in different folders

Why not use CMake for the whole project? Assuming your file structure is similar to that shown in your Visual Studio Solution Explorer, it could look something like this:

Top-level CMakeLists.txt:

cmake_minimum_required (VERSION 3.16)

# Name your VS solution.
project(MyProject)
enable_testing()

# Add the CMake file in the googletest repo
add_subdirectory(Project-A)
# Add the CMake file that configures your static library.
add_subdirectory(Project-B)
# Add the CMake file that configures your unit tests.
add_subdirectory(Project-C)

CMakeLists.txt file in Project-B folder:

# Add the VS project for the static library.
project(MyLibProj)

add_library(MyLib STATIC)

# Add the public/private sources for the static library.
target_sources(MyLib
PUBLIC
Folder-1/MyClass1.cpp
Folder-1/MyClass2.cpp
...
Folder-2/SubFolder1/UtilityClass1.cpp
Folder-2/SubFolder1/UtilityClass2.cpp
...
Folder-2/SubFolder2/HelperClass1.cpp
...
Folder-3/Circle.cpp
Folder-3/Square.cpp
PRIVATE
src/ImplClass1.cpp
src/ImplClass2.cpp
...
)

# Add the include directories for the library.
target_include_directories(MyLib
PUBLIC
${CMAKE_CURRENT_LIST_DIR}/Folder-1
${CMAKE_CURRENT_LIST_DIR}/Folder-2/SubFolder1
${CMAKE_CURRENT_LIST_DIR}/Folder-2/SubFolder2
${CMAKE_CURRENT_LIST_DIR}/Folder-3
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/include
)

CMakeLists.txt file in Project-C folder:

# Add the VS project for the executable.
project(MyExeProj)

# Create the test executable, and link the static library to it.
add_executable(MyTestExe main.cpp)
target_link_libraries(MyTestExe PRIVATE MyLib)

add_test(NAME MyTest COMMAND MyTestExe)


Related Topics



Leave a reply



Submit