-1

So I have a project called OpenGLStuffs that I am developing with Visual Studio. I am trying to do tests for this project. I created a separate test project called AppTest. In this test project I have a cpp file called AppTest.cpp that I will use to run the test version of the app. Here is the content of the test file:

AppTest.cpp

#include "pch.h"
#include "CppUnitTest.h"
#include "../openGL/OpenGLStuffs/MainApp.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace AppTest
{
    TEST_CLASS(AppTest)
    {
    public:
        
        TEST_METHOD(TestMethod1)
        {
            runApp();
        }
    };
}

Here's the content of the header file that is tested, it has a single line:

MainApp.h

const int runApp();

I already have a MainApp.cpp file that defines the runApp() function. It is too long so I’m not posting it here.

The problem is that as I tried rebuilding the AppTest project, Visual Studio is giving me this error:

Error   LNK2019 unresolved external symbol "int const __cdecl runApp(void)" (?runApp@@YA?BHXZ) 
referenced in function "public: void __thiscall AppTest::AppTest::TestMethod1(void)" (?
TestMethod1@AppTest@1@QAEXXZ)   AppTest C:\my-space\my-stuffs\AppTest\AppTest.obj   1   

I tried adding references to OpenGLStuffs to AppTests, but it doesn't seem to be working. I also tried changing the configuration type to .lib instead of .dll and the rebuilding succeeded, but this makes the test explorer to not show any tests.

Could somebody please tell me what I got wrong? Thanks in advance!

MINH TO
  • 23
  • 3

1 Answers1

0

Is the body of the function in a .cpp file, which includes that header? You can also include the body in the header itself, but then you have to declare it static or inline ... otherwise the function will be compiled multiple times causing a linking error.

JMRC
  • 1,473
  • 1
  • 17
  • 36
  • Yes! I have a .cpp file in that defines the function. It is in the same project as the .h file. – MINH TO May 21 '21 at 01:53
  • @MINHTO and did you include the header file with the declaration in the .cpp file? `#include "MainApp.h"` – JMRC May 21 '21 at 01:56
  • Yup! Everything was working fine when I called the runApp() function within the same project, but when I used it in the test project, an error was thrown. – MINH TO May 21 '21 at 01:58
  • @MINHTO: Maybe this will help: https://stackoverflow.com/questions/538134/exporting-functions-from-a-dll-with-dllexport . Check the first answer, section 'Exporting/Importing DLL Libs in VC++' – JMRC May 21 '21 at 02:09
  • Thanks. It’ll take a while but I’ll check it out. – MINH TO May 21 '21 at 02:20
  • @MINHTO It comes down to that you define `LIBRARY_EXPORTS` at the top of your .cpp files and `LIBRARY_API` in front of the functions in the .h files which you want to export. Put the `if ... else` macro in a file which is included by all DLL .h files. This way your DLL will see `__declspec(dllexport)`, while your EXE sees `__declspec(dllimport)`. – JMRC May 21 '21 at 02:30
  • We seem to be on the right track with the .lib files! I changed both of the project properties to .lib and it worked! – MINH TO May 22 '21 at 15:25
  • Great to hear! It gave me a headache as well the first time. – JMRC May 23 '21 at 01:03