1

I am making libraries, to use in multiple games so I don't have to duplicate code. In this example, my main program is main.cpp and the other two files are my libraries. Everything is linked correctly.

When all my functions in Common Functions Library.h have static in front, I get the error: static function 'std::string common::joinAll(std::vector<std::string,std::allocator<_Ty>>)' declared but not defined (Error C2129) from main.cpp (even though the functions are in the Common Functions Library.h/.cpp), and it says the line number is one more than there is in the whole program which is very strange.

So, to fix this, I found people online saying I need to replace static with inline, so I tried again and got this error: cannot open file 'Common Functions Library.lib' (Error LNK1104)

Then I tried setting all the functions back to static, and then commented out #include "Cubes Library.h" in main.cpp, which didn't give me the other errors anymore, but instead things related to the cubes library (obviously). However, this stopped on line 65, after where the functions which caused the errors saying they weren't defined. I have no clue what is going wrong, but thanks for any help :)

Code:

main.cpp

#include "Common Functions Library.h"
#include "Cubes Library.h"

// Using functions from `Common Functions Library`

Common Functions Library.cpp (Part of a DLL)

#include "Common Functions Library.h"


namespace common
{
    // Functions
}

Cubes Library.cpp (Part of a DLL)

#include "Cubes Library.h"
#include "Common Functions Library.h"
George_E -old
  • 188
  • 1
  • 3
  • 17
  • Read about exporting static exporting static function here https://stackoverflow.com/questions/2984316/dll-export-of-a-static-function – WindyFields Feb 04 '18 at 17:05
  • 1
    If you make function inline, you can place its body inside the header file, so you don't have to put it inside your DLL. – WindyFields Feb 04 '18 at 17:07
  • @WindyFields Both DLL's are made up of a header file and source file - so what shall I do? I read the SO forum, but I am still not sure – George_E -old Feb 04 '18 at 17:11
  • @WindyFields I have put all my functions in the header file, but it still says that it cannot open the .lib file ): – George_E -old Feb 04 '18 at 17:24
  • Because "cannot open file 'Common Functions Library.lib' (Error LNK1104)" probably has nothing to do with your functions. – WindyFields Feb 04 '18 at 17:25
  • "Common Functions Library" is in dynamic library (DLL according to your words) but you try to reference it as a static library - linker is angry. – WindyFields Feb 04 '18 at 17:28
  • @WindyFields I followed the Microsoft guide - what bit did I do wrong? – George_E -old Feb 04 '18 at 17:51
  • If you did everything according to *the guide* I see no way you could get such a message. And if you follow the guide, then **follow the guide**, don't make anything inline - it's a different technique. https://msdn.microsoft.com/en-us/library/ms235636.aspx – WindyFields Feb 04 '18 at 18:03
  • @WindyFields Yeah it did work with one file, but not multiple files - I'll figure it out soon :) – George_E -old Feb 04 '18 at 18:31

1 Answers1

2

If you put inline in front of all functions and define in header, there is no need for .cpp file and hence no need for .lib.

I also see namespace common in your Common Functions Library.h. So don't forget to put using namespace common; in your main.cpp or prefix common:: before all usage of class in main.cpp.

Aman
  • 696
  • 1
  • 8
  • 26