Linker error when compiling dll using Dev-C++

Posted on 2004-08-26
Last Modified: 2013-12-14
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_

# 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)




    /* 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\"
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
Question by:moronmaster
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

Expert Comment

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.
LVL 86

Expert Comment

ID: 11902448
You should declare the function as

extern "C" DLLIMPORT void HelloWorld (void);

in your header file.

Author Comment

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.

Kean Aik
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!


Expert Comment

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.

Author Comment

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.

Accepted Solution

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

#ifdef __cplusplus
extern "C"

// your function declarations

#ifdef __cplusplus

Author Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project ( automates most of the tasks discussed in this article. You can even fin…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

730 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