1

I have been able to work in the same project for sometime now, writing and successfully running c++ code. However, I discovered that I am still missing some essentials on how to export my .h files to another project and successfully use them in there.

I created a second project, project B to test the classes I have in project A. visual c++: #include files from other projects in the same solution I added the path of the header file in Project A into the Additional Include Directories(C\C++>general and Linker>general) section in the project configuration of Project B. I tried following the tutorials on this page http://msdn.microsoft.com/en-us/library/ms235636.aspx but I still end up with the error below

** LINK : fatal error LNK1104: cannot open file 'C:\Users\LaC\Projects\OSGB\Debug\OSGB.lib**

I would appreciate any help in understanding exactly how this is done so that in future, when I encounter this problem, I can know how to troubleshoot.

The code below is all I am working with.

IN PROJECT A
=============

//Utility.h
class Utility
{
    private:

    protected:

    public:
        Utility(void);
        ~Utility(void);
        double square_root (const double);
};

//Utility.cpp

    #include "StdAfx.h"
    #include "Utility.h"
    
    
    Utility::Utility(void)
    {
        //do nothing for now
    }
    
    
    Utility::~Utility(void)
    {
        //do nothing for now
    }
    
    double Utility::square_root (const double)
    {
        return 0;
    
    }

IN PROJECT B
===============

#include "gtest/gtest.h"
#include "Utility.h"

TEST (SquareRootTest, PositiveNos) { 

    Utility u; 
    EXPECT_EQ (50.3321, u.square_root (2533.310224));
}
Community
  • 1
  • 1
Kobojunkie
  • 6,375
  • 31
  • 109
  • 164

2 Answers2

1

There are two (general) ways to include files into your project:

  • Make them a part of your project (adding them from the solution explorer) OR
  • Import them as a library (static or dynamic linking)

If you make them part of your project, then you have to add the header and the source files in order for the project to compile correctly. However, that's usually not what you want to do, as it defeats the purpose of having external libraries.

The second case is to use the external libraries, which requires that you:

  • Include the header files which are exported by the library in your C++ properties.
  • For static linking: you also have to include the *.lib file (the output of building the library) in the Linker properties.

OR


So remember: there are two parts to building a C++ project- compiling and linking.

Compiler Errors:
If you get an error whose code starts with C* (e.g. C1083) and is related to problems header with the files, then check the Properties-> C/C++ -> General -> Additional Include Directories.

Linker Errors:
If you get an error whose code starts with LNK*, then check

  • Properties -> Linker -> General -> Additional Library Directories (make sure that this points to where the *.lib file is located)
    AND
  • Properties -> Linker -> Input -> Additional Dependencies (make sure that the *.lib file is added here).

If you're dynamically linking, then check that you're correctly referencing the DLL.


So in your case, you have to determine if you're linking statically or dynamically and then make the appropriate references. So ware you getting those header files from a dynamically library or a static library?

Kiril
  • 39,672
  • 31
  • 167
  • 226
  • I am trying to do both, in the same project. I first build the google test files and added the static lib files, and this built correctly. I tried linking to a *.dll from another project, within the same solution file. This is where I am stuck at this point. The error message seems to point to some problem with importing the *.lib file, only I have no *.lib file for that project – Kobojunkie Jun 08 '12 at 23:47
  • And I am working with the assumption that when trying to do dynamic linking, I use *.dll files, while for static linking, I use *.lib files. The error is LINK : fatal error LNK1104: cannot open file 'C:\Users\LaC\Projects\OSGB\Debug\OSGB.lib' – Kobojunkie Jun 08 '12 at 23:51
0

When the linker emits unresolved external symbol for a symbol that lives in another library (DLL or shared library), this indicates that you need to link your app to that other library's .lib file. That is most likely what's happening here.

For more information see:

(MSDN) Walkthrough: Creating and Using a Dynamic Link Library (C++)

John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • Thanks for the link. I read through and followed the steps in that list. But I seem to still have one more linker issue to deal with. The new error is >LINK : fatal error LNK1104: cannot open file 'C:\Users\LaC\Projects\OSGB\Debug\OSGB.lib'. I don't see a .lib file in my debug folder though – Kobojunkie Jun 08 '12 at 23:20
  • anyone able to help? I have been pulling at this for hours now. – Kobojunkie Jun 09 '12 at 03:50