Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Linker error when compiling dll using Dev-C++

Posted on 2004-08-26
7
Medium Priority
?
862 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 1000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

604 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