locked
Even though the Native Unit Test project referred other projects, VS17 throws compile errors. RRS feed

  • Question

  • Hi, I was practicing the C++ Native Unit Test project with VS17(x86, v15.2) on my 64-bit Windows 10 machine.

    Visual C++ Team Blog - C++ Unit Testing in Visual Studio

    That blog post was the one I was referencing.

    Simple tests like

    Assert::AreEqual(1, 1);
    Assert::AreNotEqual(7, 8);

    were buildable, so test results were shown.

    Now, I was trying to test my custom class which was created on a Win32 Application project.

    // foo.cpp in win32Project
    #include <windows.h>
    #include "foo.h"
    
    void Foo::printMSG()
    {
    	MessageBox(NULL, LPCWSTR(this->msg), L"Title", MB_OK);
    }
    
    int Foo::getAdd(int x, int y)
    {
    	return x + y;
    }
    
    int Foo::getMul(int x, int y)
    {
    	return x * y;
    }
    
    // foo.h in win32Project
    #pragma once
    #include <string.h>
    #include <tchar.h> 
    // #include <winnt.h> // LPCWSTR
    
    class Foo {
    private:
    	LPCWSTR msg;
    
    public:
    	explicit Foo(LPCWSTR m)
    	{
    		this->msg = m;
    	}
    	void printMSG();
    
    	int getAdd(int x, int y);
    	
    	int getMul(int x, int y);
    };
    
    // unittest1.cpp
    #include "stdafx.h"
    #include "CppUnitTest.h"
    #include "../win32Project/foo.h"
    
    //#include <winnt.h>
    
    using namespace Microsoft::VisualStudio::CppUnitTestFramework;
    
    namespace UnitTest0
    {		
    	TEST_CLASS(UnitTest0)
    	{
    	public:
    		
    		TEST_METHOD(TestMethod0)
    		{
    			Foo test(L"MS Unit Test");
    
    			Assert::AreEqual(7, test.getAdd(3, 4));
    
    			Assert::AreNotEqual(8, test.getAdd(2, 4));
    
    			
    			Assert::AreEqual(1, 1);
    			Assert::AreNotEqual(7, 8);
    		}
    
    	};
    	/*
    	TEST_CLASS(UnitTest1)
    	{
    	public:
    
    		TEST_METHOD(TestMethod1)
    		{
    
    			Assert::AreEqual(1, 1);
    			Assert::AreEqual(7, 8);
    			Assert::AreNotEqual(7, 8);
    		}
    
    	};
    	*/
    }

    If I try to build this, VS17 throws compile errors.

    2>c:\users\PROJECT\win32Project\foo.h(8): error C3646: 'msg': unknown override specifier
    2>c:\users\PROJECT\win32Project\foo.h(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    2>c:\users\PROJECT\win32Project\foo.h(11): error C2061: syntax error: identifier 'LPCWSTR'
    2>c:\users\PROJECT\win32Project\foo.h(13): error C2039: 'msg': is not a member of 'Foo'
    2>c:\users\PROJECT\win32Project\foo.h(6): note: see declaration of 'Foo'
    2>c:\users\PROJECT\win32Project\foo.h(13): error C2065: 'm': undeclared identifier
    2>c:\users\PROJECT\unittest0\unittest1.cpp(17): error C2664: 'Foo::Foo(Foo &&)': cannot convert argument 1 from 'const wchar_t [13]' to 'const Foo &'
    2>c:\users\PROJECT\unittest0\unittest1.cpp(17): note: Reason: cannot convert from 'const wchar_t [13]' to 'const Foo'
    2>c:\users\PROJECT\unittest0\unittest1.cpp(17): note: No constructor could take the source type, or constructor overload resolution was ambiguous
    2>Done building project "UnitTest0.vcxproj" -- FAILED.

    Is UnitTest0 project referring the class correctly?

    Since the original project(win32Project) compiled and build before adding this unit test project,

    I'm confused why these errors pop up. Can you help me out?


    -Best Regards

    Monday, July 10, 2017 6:22 AM

Answers

  • Hi friend,

    Sorry for the delay.

    Finally, I think we made it. The key point is to “export” the functions/classes you want to test. Please use _declspec(dllexport), refer to following steps on how to add an exe as a unit test target.

    Note: My sample is not include the MFC.

    1. Put the declaration of the methods into the header (foo.h) file, and also export these functions.

    #ifdef BambooTest_EXPORTS

    #define BambooTest_API __declspec(dllexport)

    #else

    #define BambooTest_API __declspec(dllimport)

    #endif

     

    class BambooTest_API Foo {}

    1. Include the header and the lib to the "unittest1.cpp" file (adopt the paths as you want).

    #include "../BambooTest/foo.h"

    #pragma comment(lib, "C:\\Users\\[user]\\Downloads\\BambooTest (3)\\BambooTest\\x64\\Debug\\BambooTest.lib")

    1. Set unit test property.

    Right click unit test project -> choose Properties -> extendC/C++ node-> General, setAdditional Include Directories is“$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)” ->Apply

    1. Right click solution, Clean Solution, Rebuild Solution. Check the result.

    Here is OneDrive link where you could download the project.

     

     

    Best regards,

    Fletcher


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Tuesday, July 18, 2017 9:57 AM
    • Proposed as answer by Fletch Zhou Wednesday, July 19, 2017 8:08 AM
    • Marked as answer by DeveloperCCC Thursday, July 20, 2017 3:56 AM
    Tuesday, July 18, 2017 9:57 AM
  • Hi friend,

    >> I was asking about these (declspec, #pragma comment).

    >>Do these solved the compile/build errors for testing member functions?

    Yes, they are solved the compile/build errors for testing member functions.

    The declarator __declspec(dllexport) causes the public and protected members of the class to be visible outside the DLL.

    For more information, please see https://msdn.microsoft.com/en-us/library/9h658af8.aspx

    Use “#pragma comment” can automatically link the tests to the required object or library files. Please refer to “To link the tests to the object or library files”in document below:

    https://msdn.microsoft.com/en-us/library/hh419385.aspx?f=255&MSPPError=-2147217396#objectRef

    >> As you commented the Win32 parts of the code, the errors still show up when I uncomment it.

    Did you mean you use the MFC (messagebox) in win32 projects?

    If yes, I suggest you ask help in C++ forum where you will get more professional answer. Since our forum is to discuss unit testing, thanks for your understanding.

     

    Best regards,

    Fletcher

     

     


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Thursday, July 20, 2017 3:35 AM
    • Marked as answer by DeveloperCCC Thursday, July 20, 2017 3:56 AM
    Thursday, July 20, 2017 3:35 AM

All replies

  • Hi friend, 

    Thanks for your post. 

    Just wonder if you've added win32project under test as Reference to your unit test project? Like below:

    Also, may I ask if you could compile the win32project successfully, I mean compile it separately without unit test project? 

     

     

    Best regards, 

    Fletcher 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Tuesday, July 11, 2017 11:01 AM
    Tuesday, July 11, 2017 11:00 AM
  •  

    0) Just wonder if you've added win32project under test as Reference to your unit test project? Like below:

    1) Also, may I ask if you could compile the win32project successfully, I mean compile it separately without unit test project? 

    Hello, Fletcher. Good to see you again!

    0) I have added it.

    If I right-mouse click the "References", which is in the "UnitTest0" project, and click "Add Reference...",

    the above result is observed.

    However, the same compiler error is still shown.

    1) I wish I have understood your intention of "compile it separately".

    I clicked "Rebuild" and the below output logs are the result.

    1>------ Rebuild All started: Project: Win32project, Configuration: Debug x64 ------
    1>main.cpp
    1>foo.cpp
    1>Generating Code...
    1>Win32project.vcxproj -> C:\Users\USERNAME\Documents\Visual Studio 2017\Projects\Win32project\x64\Debug\Win32pRoject.exe
    1>Win32project.vcxproj -> C:\Users\USERNAME\Documents\Visual Studio 2017\Projects\Win32project\x64\Debug\Win32pRoject.pdb (Partial PDB)
    ========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

    So I think this project itself is compilable and buildable.

    Hmm, should I send you this project by uploading it here or email?


    -Best Regards


    Wednesday, July 12, 2017 12:49 AM
  • Hi friend,

    Thanks for your feedback.

    Yes, I think it'd be better if you could share the project with us. And I'll test it in my side to help you out.

    According to MSDN policy, I'm allowed to post personal email, but you could upload your project to OneDrive, and post the link here, then I will download it.

     

    Best regards,

    Fletcher


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Wednesday, July 12, 2017 8:17 AM
    Wednesday, July 12, 2017 8:17 AM
  • you could upload your project to OneDrive, and post the link here, then I will download it.

    Hi, Fletcher.

    OneDrive Share Link

    I have cleaned the solution and made a zip file.

    Can you take a look, please?


    -Best Regards

    Thursday, July 13, 2017 12:02 AM
  • Dear friend,

    Thanks for your cooperation.

    Finally, I fixed it I think.

    After add #include <windows.h> in your foo.h file, I got the error LNK2001,

    This error can be caused in a project that uses function inlining if you define the functions in a source file rather than in a header file.

    Then I put functions shown below in foo.h instead of foo.cpp file:

         int Foo::getAdd(int x, int y)
         {
             return x + y;
         }
    
         int Foo::getMul(int x, int y)
         {
             return x * y;
         }

    After I could run the test and got the expected result.

    and here is project after I modified, you could download here:

    new_Project

     

    Best regards,

    Fletcher


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Thursday, July 13, 2017 11:05 AM
    Thursday, July 13, 2017 11:05 AM
  • After add #include <windows.h> in your foo.h file, I got the error LNK2001,

    This error can be caused in a project that uses function inlining if you define the functions in a source file rather than in a header file.

    Then I put functions shown below in foo.h instead of foo.cpp file:

         int Foo::getAdd(int x, int y)
         {
             return x + y;
         }
    
         int Foo::getMul(int x, int y)
         {
             return x * y;
         }

    Hi, Fletcher! I have downloaded your new project and it builds well on my side, too.

    However, it is quite confusing.

    Header files allow you to make the interface visible to other .cpp files

    while keeping the implementation in its own .cpp file.

    Ref. Link

    I might be wrong; usually, the C++ code is implemented in the cpp / cc files, not header files, isn't it?

    More importantly, I don't see a big difference about implementing same member functions in header files or cpp files.

    Is implementing my C++ codes inside header files, not in cpp or cc files, the only solution to solve the above compile errors?

    Why does the compiler errors show up when implementing codes inside cpp files?

    Although this can solve my problem now, I think this cannot be the best solution for later.


    -Best Regards


    • Edited by DeveloperCCC Friday, July 14, 2017 5:20 AM Removed Grammarly Tags
    Friday, July 14, 2017 5:19 AM
  • Hi friend,

    Sorry for the delay.

    Finally, I think we made it. The key point is to “export” the functions/classes you want to test. Please use _declspec(dllexport), refer to following steps on how to add an exe as a unit test target.

    Note: My sample is not include the MFC.

    1. Put the declaration of the methods into the header (foo.h) file, and also export these functions.

    #ifdef BambooTest_EXPORTS

    #define BambooTest_API __declspec(dllexport)

    #else

    #define BambooTest_API __declspec(dllimport)

    #endif

     

    class BambooTest_API Foo {}

    1. Include the header and the lib to the "unittest1.cpp" file (adopt the paths as you want).

    #include "../BambooTest/foo.h"

    #pragma comment(lib, "C:\\Users\\[user]\\Downloads\\BambooTest (3)\\BambooTest\\x64\\Debug\\BambooTest.lib")

    1. Set unit test property.

    Right click unit test project -> choose Properties -> extendC/C++ node-> General, setAdditional Include Directories is“$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)” ->Apply

    1. Right click solution, Clean Solution, Rebuild Solution. Check the result.

    Here is OneDrive link where you could download the project.

     

     

    Best regards,

    Fletcher


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Tuesday, July 18, 2017 9:57 AM
    • Proposed as answer by Fletch Zhou Wednesday, July 19, 2017 8:08 AM
    • Marked as answer by DeveloperCCC Thursday, July 20, 2017 3:56 AM
    Tuesday, July 18, 2017 9:57 AM
  • Finally, I think we made it. The key point is to “export” the functions/classes you want to test. Please use _declspec(dllexport), refer to following steps on how to add an exe as a unit test target.

    Note: My sample is not include the MFC.

    1. Put the declaration of the methods into the header (foo.h) file, and also export these functions.

    #ifdef BambooTest_EXPORTS

    #define BambooTest_API __declspec(dllexport)

    #else

    #define BambooTest_API __declspec(dllimport)

    #endif

     

    class BambooTest_API Foo {}


    Hello, Fletcher! Thanks for your endeavor.

    Well, I'm a novice C++ programmer. So I think I have to study about 

    declspec

    and

    #pragma comment

    In the meantime, about "My sample is not include the MFC", I see;

    If I remove the comments, it generates compiler/linker errors again.

    int Foo::getAdd(int x, int y)
    {
    	return x + y;
    }

    Well, it is a good news that those simple functions, written in Cpp files, are testable with the Native Unit Test project.

    Then, hmm...

    As you remember, your first solution was to type the C++ codes in header files.

    Does

    declspec

    and

    #pragma comment

    made it possible to write C++ codes in the Cpp files? Can you give a brief elaboration about roles of those?



    -Best Regards

    Wednesday, July 19, 2017 9:07 AM
  • Hi friend,

    Glad to receive you response.

    >> Can you give a brief elaboration about roles of those?

    C++ classes (and often function prototypes) are normally split up into two files. 

    The header file has the extension of .h and contains class definitions and functions. The implementation of the class goes into the .cpp file. By doing this, if your class implementation doesn’t change then it won’t need to be recompiled.

    Most IDE’s will do this for you – they will only recompile the classes that have changed. This is possible when they are split up this way, but it isn’t possible if everything is in one file (or if the implementation is all part of the header file).

    Please feel free to let me know if you need more help with it.

     

    Best regards,

    Fletcher


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Wednesday, July 19, 2017 10:07 AM
    Wednesday, July 19, 2017 10:06 AM

  • The header file has the extension of .h and contains class definitions and functions. The implementation of the class goes into the .cpp file. By doing this, if your class implementation doesn’t change then it won’t need to be recompiled.

    Most IDE’s will do this for you – they will only recompile the classes that have changed. 

    Hi, Fletcher.

    You mean 

    declspec

    and

    #pragma comment has relation with header files and Cpp files?

    I was asking about these(declspec, #pragma comment).

    Do these solved the compile/build errors for testing member functions?

    Also, the Win32 is still a problem.

    As you commented the Win32 parts of the code, the errors still show up when I uncomment it.

    When working with Win32 projects, is there any solution to solve compile/build errors which are created from the Native C++ Unit Test project?

    Or is this the limitation of the Native C++ Unit Test project that it cannot test Win32 codes?


    -Best Regards

    Wednesday, July 19, 2017 12:53 PM
  • Hi friend,

    >> I was asking about these (declspec, #pragma comment).

    >>Do these solved the compile/build errors for testing member functions?

    Yes, they are solved the compile/build errors for testing member functions.

    The declarator __declspec(dllexport) causes the public and protected members of the class to be visible outside the DLL.

    For more information, please see https://msdn.microsoft.com/en-us/library/9h658af8.aspx

    Use “#pragma comment” can automatically link the tests to the required object or library files. Please refer to “To link the tests to the object or library files”in document below:

    https://msdn.microsoft.com/en-us/library/hh419385.aspx?f=255&MSPPError=-2147217396#objectRef

    >> As you commented the Win32 parts of the code, the errors still show up when I uncomment it.

    Did you mean you use the MFC (messagebox) in win32 projects?

    If yes, I suggest you ask help in C++ forum where you will get more professional answer. Since our forum is to discuss unit testing, thanks for your understanding.

     

    Best regards,

    Fletcher

     

     


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Thursday, July 20, 2017 3:35 AM
    • Marked as answer by DeveloperCCC Thursday, July 20, 2017 3:56 AM
    Thursday, July 20, 2017 3:35 AM
  • Did you mean you use the MFC (messagebox) in win32 projects?

    If yes, I suggest you ask help in C++ forum where you will get more professional answer. Since our forum is to discuss unit testing, thanks for your understanding.


    Hi, Fletcher! Then I will post a question at the C++ forum.

    Can you leave the Onedrive link (Update BambooTest.zip) opened so others can use it to examine?


    -Best Regards

    Thursday, July 20, 2017 4:27 AM
  • Sure, I will leave the OneDrive link there and keep the project in my side just in case.  

     

    Best regards,

    Fletcher


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Thursday, July 20, 2017 5:28 AM
    Thursday, July 20, 2017 5:28 AM