Solved

Linker error when compiling dll using Dev-C++

Posted on 2004-08-26
7
849 Views
Last Modified: 2013-12-14
Hi,
I have been trying to learn how to write dll's but i can't seem to even get started. I am using Dev-C++ and i basically used the sample template provided to generate a simple hello world dll as follows:
#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 */


extern __declspec (dllexport) void HelloWorld (void);


#endif /* _DLL_H_ */

/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

__declspec (dllexport) void HelloWorld ()
{
    MessageBox (0, "Hello World from DLL!\n", "Hi", MB_ICONINFORMATION);
}


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;
}

This compiles and i get a .dll and a .a file.

Next i create a sample GUI application again from Dev-C++ but when i compile i get  the error:  
[Linker error] undefined reference to `HelloWorld()'

I already remembered to include #include <dll.h> at the top of the main.cpp file. And i already imported the .a file already so what am i missing?

The compiler message that i get is as follows:
Compiler: Default compiler
Building Makefile: "D:\general\sw\code\workfolder\Makefile.win"
Executing  make clean
rm -f obs/main.o  obs/Project1.exe

g++.exe -c main.cpp -o obs/main.o -I"C:/dev_cpp/include/c++"  -I"C:/dev_cpp/include/c++/mingw32"  -I"C:/dev_cpp/include/c++/backward"  -I"C:/dev_cpp/include"  -I"D:/general/sw/code/workfolder"   -ansi

g++.exe obs/main.o  -o "obs\Project1.exe" -L"C:/dev_cpp/lib" -L"D:/general/sw/code/workfolder" -mwindows  libmydll.a

obs/main.o(.text+0x15a):main.cpp: undefined reference to `HelloWorld()'

make.exe: *** [obs/Project1.exe] Error 1

Execution terminated
0
Comment
Question by:moronmaster
  • 3
  • 3
7 Comments
 
LVL 2

Expert Comment

by:carribus
ID: 11901375
I'm not sure how Dev-C++ does it, but I can relate to you the way I do in Visual Studio, and maybe it will throw you on the right track.

Basically, you can use DLL's in 2 ways. You can statically link them to your consumer code (i.e. the gui), or you can dynamically link them through the GetProcAddress() function in the Win32 API.

By the sound of it, you're trying to do a static link. When I need to do a statically linked library, I need to use the .lib file that was generated when compiling the dll and add that to the linked files when compiling the "consumer" app.
0
 
LVL 86

Expert Comment

by:jkr
ID: 11902448
You should declare the function as

extern "C" DLLIMPORT void HelloWorld (void);

in your header file.
0
 

Author Comment

by:moronmaster
ID: 11909689
i finally figured out my problem.
I was writing the DLL using C, so i couldn't put in the extern "C" statement as somehow that would generate compile errors.
However when i try to test the DLL i was using C++ and since the header file did not have the extern "C" statement it gave a linker error "undefined reference to `HelloWorld()'".

I hope you guys don't mind if i extend the question a bit more. What should i do to be able to compile the DLL in C and yet be able to call it from a program compiled in C++? Is there any difference if  i compile the DLL in C and if i compile it in C++ (i am not using classes at all).

PS.: i am using statically linked library since its easier.

Thanks
Kean Aik
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 2

Expert Comment

by:carribus
ID: 11911027
shouldn't be a problem... just remember that in the C++ app, when you use the .h file for your imported functions, that the functions should be wrapped with extern "C".

Apart from that, it should work fine.
0
 

Author Comment

by:moronmaster
ID: 11929302
The problem is when i use extern "C" in the header and try to compile the dll using a C compiler, i get compile errors.
One way i worked around the issue is to explicitly declare the function i wanted to use as extern "C" rather than include the dll.h file.
0
 
LVL 2

Accepted Solution

by:
carribus earned 250 total points
ID: 11929317
try this...

#ifdef __cplusplus
extern "C"
{
#endif

// your function declarations

#ifdef __cplusplus
}
#endif
0
 

Author Comment

by:moronmaster
ID: 11988423
Sorry for closing this so late. I tried out your suggestion and it works. Thanks.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question