Solved

Linker error when compiling dll using Dev-C++

Posted on 2004-08-26
7
857 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

688 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