DLL Making and Symbols - 2 Queries

This is a discussion on DLL Making and Symbols - 2 Queries within the C++ Programming forums, part of the General Programming Boards category; My first question, is about DLL creation in C++. I've heard about what DLLs can do for a program, and ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    220

    DLL Making and Symbols - 2 Queries

    My first question, is about DLL creation in C++. I've heard about what DLLs can do for a program, and now I want to implement them in my own programs. I've been thinking of using them first with a simple console-based hello world function, then going onto more advanced functions from this. But regaurdless, do you have any resources that I might be able to look into for coding DLLs in C++? I haven't found any on this board from a search, besides a few questions regaurding errors and the like, but no code was presented in any of these cases, to my dismay. I'de like to take my coding to the next level, and I figure knowing how to code DLLs will get me there.

    The second question, has no examples. But it can be simply put: In C++, what are symbols? I've seen this term used frequently in compiler errors, and possibly linker errors, however don't quote me on that. The question popped up in my head while browsing through some CS Mod boards.

    Thank you for your time,
    ~Tronic
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    The second question, has no examples. But it can be simply put: In C++, what are symbols? I've seen this term used frequently in compiler errors, and possibly linker errors, however don't quote me on that. The question popped up in my head while browsing through some CS Mod boards.
    In the real world, symbols represent other things. A swatstika for example. In C++, things such as variable name, etc... represent things. These are symbols.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    That answers my question about symbols, but what about DLLs? It seems there aren't many that know about them, at least those who browse the C++ threads, haven't gotten a reply in a while.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    You might want to do a board search for DLLs and look for a thread started by Rune Hunter. That thread should walk you throug the whole thing.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Thank you Govtcheez

    and thus, I have come to one more question about DLLs. Very interesting things these are, infact.

    Here's my DLL:

    dll.h
    Code:
    #include "dll.h"
    #include <windows.h>
    #include <iostream>
    DllClass::DllClass()
    {}
    DllClass::~DllClass ()
    {}
    int DllClass::helloWorld()
    {
      std::cout << "Hello World!";
      return 0;
    }  
    BOOL APIENTRY DllMain 
    (
    HINSTANCE hInst     
    /* Library instance handle. */ ,
    DWORD reason        /* Reason this function is being called. */ ,
    LPVOID reserved     /* Not used. */
    )
    {
        switch (reason)
        {
          case DLL_PROCESS_ATTACH:
            break;
          case DLL_PROCESS_DETACH:
            break;
          case DLL_THREAD_ATTACH:
            break;
          case DLL_THREAD_DETACH:
            break;
        }
        /* Returns TRUE on success, FALSE on failure */
        return TRUE;
    }
    dllmain.cpp

    Code:
    #ifndef _DLL_H_
    #define _DLL_H_
    #if BUILDING_DLL
    # define DLLIMPORT __declspec (dllexport)
    #else /* Not BUILDING_DLL */
    # define DLLIMPORT __declspec (dllimport)
    #endif /* Not BUILDING_DLL */
    class DLLIMPORT DllClass
    {
      public:
        DllClass();
        virtual ~DllClass(void);
        int helloWorld();
    };
    #endif /* _DLL_H_ */
    Here's my program that I want to call the DLL correctly:

    main.cpp
    Code:
    #include <stdlib.h>
    #include <windows.h>
    
    int main(int argc, char *argv[])
    {
      LoadLibrary("dll.dll");
      if(DllClass::helloWorld() = 0)
        std::cout << "\nSuccess!";
      else
        std::cout << "Output failed for some weird reason~!";
      system("PAUSE");	
      return 0;
    }
    It gives me: helloWorld() undeclared (first use this function), and yet helloWorld is in my DLL. I've also tried DllClass::helloWorld(), btw.
    Last edited by Tronic; 12-30-2004 at 07:53 PM.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Well, there's gonna be a problem with this..

    if(DllClass::helloWorld() = 0)

    You're trying to assign 0 to DllClass::helloWorld(), not check if helloWorld() returned 0.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Tried that before. It's saying 'Undeclared helloWorld() first use of this function'.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,700
    you need to include the header and declare an object of the class.

    Code:
    #include <stdlib.h>
    #include <windows.h>
    #include "dll.h"
    int main(int argc, char *argv[])
    {
     DllClass dc;
     dc.helloWorld();
     system("PAUSE");	
     return 0;
    }
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Ahh, that was an..interesting mistake lol. Thanks

    Only problem I have so far is with the linker..which is interesting.

    I've edited my main.cpp code to show the resulting edit. My DLLs code hasn't changed.

    The errors it gives me are:

    [Linker error] undefined reference to `_imp___ZN8DllClassC1Ev'

    [Linker error] undefined reference to `_imp___ZN8DllClass10helloWorldEv'

    [Linker error] undefined reference to `DllClass::~DllClass()'

    I'm not that good with linkers so...
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,700
    go to project options and add the library file (with the .a extension) to the linker options.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. symbols returned when reading file
    By LowLife in forum C Programming
    Replies: 3
    Last Post: 06-10-2006, 11:12 AM
  2. Undefined symbols from ld? ; making bundle; gcc 3.3
    By profxtjb in forum C Programming
    Replies: 5
    Last Post: 06-15-2004, 01:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21