-1

I'm using Visual Studio 2017. I wanted to practice adding a static library project to an existing solution. The hierarchy looks like this:

Solution:

  • Console Practice (C++ console project) - (Files: main.cpp)

  • MathLib (Static library project) - (Files: mathlib.h, mathlib.cpp)

Both compiled fine.

Now I need them to talk to each other.

I want 'Console Practice' to call a function from 'MathLib', such as add(n, n).

I noticed I can't easily do this with just calling the function. 'Console Practice' doesn't recognize 'MathLib.h'.

How can I call add(n,n)?

Edit:

I tried going by this solution in the link and added the path to the lib, and added MathLib.lib as a reference to 'Console Project', but it doesn't recognize my #include "mathlib.h" file in the static library project. I have to make a copy of this header file, which is undesirable. Can I just access the header instead of copying the latest version into my 'Console Practice' project?

Adding a static library to a project in the same solution (Visual Studio 2012)

Bob
  • 327
  • 3
  • 12
  • Possible duplicate of [How do I add additional libraries in C++?](https://stackoverflow.com/questions/4445418/how-do-i-add-additional-libraries-in-c) – Richard Critten Jul 15 '18 at 16:13
  • Possible duplicate of [Problems importing libraries to my c++ project, how to fix this?](https://stackoverflow.com/questions/24715864/problems-importing-libraries-to-my-c-project-how-to-fix-this) – πάντα ῥεῖ Jul 15 '18 at 16:13
  • Those are still not solving the header issue. Additional Include Directories doesn't recognize the header even though it's set to the library's path with mathlib.h. – Bob Jul 15 '18 at 16:28
  • Make sure you set the additional include directories for both Debug and Release configurations and that your path is correct. – Mark Tolonen Jul 15 '18 at 16:39
  • It was a platform conflict with x64 project settings with the main project set to x86. – Bob Jul 16 '18 at 20:20

1 Answers1

1

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
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • #include "../mathlib/mathlib.h" works. However, when I add "../mathlib" to Additional Include Directories, including "mathlib.h" causes a header not found error. Thinking it was due to the build order, I compiled MathLIb project first, and then my main project, but the header still cannot be found using the latter example. – Bob Jul 16 '18 at 06:23
  • Adding the full header directory path to Additonal Include Directories also caused a no header found error. Is it because the default static library project settings adds a "#pragma once" line to it? So far I don't think that's the case. – Bob Jul 16 '18 at 06:33
  • Last point: As an experiment, I copied the header file into my C:\Libaries\ directory and it still couldn't find it on compile. – Bob Jul 16 '18 at 06:58
  • Curious if it's because the default static library project adds stdafx.h/.cpp. – Bob Jul 16 '18 at 07:04
  • @Bob Did you make sure to set Additional Includes on both the Debug and Telease configurations? – Mark Tolonen Jul 16 '18 at 14:08
  • I am only working in debug because there is no release build yet. – Bob Jul 16 '18 at 14:16
  • @Bob Then, did you set it in Release and not Debug? It's still a good idea select "All Configurations" when setting a project property or when you do switch to Release and the settings are inconsistent you'll have more to debug. There really isn't any other reason using this setting and not a relative path in the #include should fail. I verified both methods with VS to make sure I was specifying the dialog paths correctly. If you still have issues, using a tool like SysInternals Process Monitor to see the exact path Visual Studio is accessing can help. – Mark Tolonen Jul 16 '18 at 15:38
  • Solution was to make sure the platform was all x64. The project settings were for x64 but one of the projects at least was set to x86. Thanks for the help. – Bob Jul 16 '18 at 16:29