CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2017
    Posts
    2

    Dll Application Error

    ===============Program===============
    --->Main.cpp<---
    Code:
    #include "Dll_VitexMaths.h"
    #include <iostream>
    
    void SimpleMaths();
    
    int main() {
    	/* Check For Memory Leaks */
    	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    
    	SimpleMaths();
    
    	return EXIT_SUCCESS;
    }
    
    template<typename T>
    void SimpleMaths() {
    	Dll_VitexMaths<T>* VitexMaths<T> = new Dll_VitexMaths<T>();
    	T VM_Value;
    	T VM_MultiplyBy;
    
    	/* Main Code */
    	std::cout << "Enter a Number: ";
    	std::cin >> VM_Value;
    	std::cout << "Multiply " << VM_Value << " By: ";
    	std::cin >> VM_MultiplyBy;
    
    	std::cout << "Your End Value Is: " << VitexMaths<T>->Multiply(VM_Value, VM_MultiplyBy) << "\n";
    
    	system("PAUSE >NULL");
    
    	delete VitexMaths;
    }
    ===============Dll===============
    --->Dll_VitexMaths.cpp<---
    Code:
    #include "Dll_VitexMaths.h"
    
    template<typename T>
    T Dll_VitexMaths<T>::Multiply(const T value, const T multiplyBy) {
    	value *= multiplyBy;
    
    	return value;
    }
    ===============Dll===============
    --->Dll_VitexMaths.h<---
    Code:
    #ifndef Dll_VitexMaths_H
    #define Dll_VitexMaths_H
    
    template<typename T>
    class __declspec(dllexport) Dll_VitexMaths {
    public:
    	T Multiply(const T value, const T multiplyBy);
    };
    
    #endif /* Dll_VitexMaths_H */
    ================================

    Hello, I've made a simple program which uses a Dll to do math stuff like multiplying integers, but then I decided to make it a template and now I have the error:
    Severity Code Description Project File Line Suppression State
    Error LNK2019 unresolved external symbol "void __cdecl SimpleMaths(void)" (?SimpleMaths@@YAXXZ) referenced in function _main Simple Maths L:\C++\PROGRAMS\Simple Maths\Simple Maths\Main.obj 1

    from the application that uses the Dll, the error isn't in the Dll I don't think, I may have done something wrong in my program.
    The Dll_VitexMaths.cpp/.h is in the project/Source/Dll_VitexMaths/ so I'm not sure if it's reading it correctly, I also included the Dll cpp file in my program and that didn't change anything, any help? Thanks
    Also quickly, everything is linked properly.
    Last edited by 2kaud; September 21st, 2017 at 03:35 PM.

  2. #2
    Join Date
    Sep 2017
    Posts
    2

    Re: Dll Application Error

    I hate how this website doesn't indent -.-

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Dll Application Error

    Quote Originally Posted by VitexHD View Post
    I hate how this website doesn't indent -.-
    You should hate yourself because you didn't read the Announcement, Information on posting and Including Code sections.
    Victor Nijegorodov

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Dll Application Error

    Simply, you don't put templated code in a .dll. The templated code needs to be in the same compilation unit as the code that invokes it. As Simplemaths() invokes templated VitexMaths(), then they both need to be in the same compilation unit.

    Also note that SimpleMaths() is a templated function, but its forward declaration is not.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Dll Application Error

    Quote Originally Posted by VitexHD View Post
    Hello, I've made a simple program which uses a Dll to do math stuff like multiplying integers, but then I decided to make it a template and now I have the error:
    Severity Code Description Project File Line Suppression State
    Error LNK2019 unresolved external symbol "void __cdecl SimpleMaths(void)" (?SimpleMaths@@YAXXZ) referenced in function _main Simple Maths L:\C++\PROGRAMS\Simple Maths\Simple Maths\Main.obj 1
    The thing is you cannot place a template into dll unless it's some instantiated template class or a few ones. You place in dll the code that was able to build, but not an abstract template that is to be turned to real code at compile time. Just because the dll compile time is history when you load the dll at runtime.

    The thing is pretty obvious, but it rarely occurs to C++ beginners. Now when you are properly informed you are to revise your approach.
    Best regards,
    Igor

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured