Solved

Linker error when compiling dll using Dev-C++

Posted on 2004-08-26
7
835 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
Comment Utility
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
Comment Utility
You should declare the function as

extern "C" DLLIMPORT void HelloWorld (void);

in your header file.
0
 

Author Comment

by:moronmaster
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 2

Expert Comment

by:carribus
Comment Utility
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
Comment Utility
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
Comment Utility
try this...

#ifdef __cplusplus
extern "C"
{
#endif

// your function declarations

#ifdef __cplusplus
}
#endif
0
 

Author Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now