Code::Blocks and Mingw32 - linking to static libraries not working. Help!

Hello,

Recently I decided to try Code::Blocks in conjunction with Mingw32 for a new project (This is on Windows). As I hadn't used Code::Blocks much before, I created a test program, which required linking a static library (dlfcn-win32 in this case).

The linker search path includes the path to libdl.a, and libdl is included in the project's linker settings. However, when I compile this code, I get the error:

1
2
3
4
5
Compiling: main.cpp
Linking console executable: bin\Debug\test.exe
obj\Debug\main.o: In function `main':
C:/**********/main.cpp:8: undefined reference to `dlopen(char const*, int)'
collect2: ld returned 1 exit status


This is strange, as I have linked against libdl. The source code is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <dlfcn.h>
#include <cstdlib>

int main(int argc, char** argv)
{
    std::cout << "herp" << std::endl;
    void* handle = dlopen("test.dll", RTLD_LAZY);
    if (handle == NULL)
    {
        std::cout << "derp\n";
        exit(1);
    }
    std::cout << "test\n";
    system("pause");
    return 0;
}
Can't you just use LoadLibrary()?
The actual project will be cross-platform (hence why I'm using Code::Blocks and GNU tools), and will load a shared library at runtime. I'd like to be able to use dlfcn functions on all platforms, and dlfcn-win32 does the trick.

Anyhow, I'll still be linking against other static libraries in the project, namely SDL. I have a feeling this problem is not libdl-specific. I know the library is fine, for I compiled it myself using Mingw.
Hmm...

The linker search path includes the path to libdl.a, and libdl is included in the project's linker settings

The linker not only needs to know the path, but also the full name of the lib i.e. "libdl.a". Did you provide that?

Edit: quote
Last edited on
I know the library is fine, for I compiled it myself using Mingw.

Have you tried compiling your example from the console and see if that works?
@Caligulaminus:
GCC only requires -ldl, as -l is the option for adding a library, and 'dl' automatically means 'libdl.a'; Code::Blocks resolves dl, libdl, and libdl.a to '-ldl' in the linker call.

@hanst99:
Unfortunately, I tried and got this:

C:\test\src>g++ -I..\include -o text.exe main.cpp -L..\lib -ldl
C:\Users\Torin\AppData\Local\Temp\ccHLlwwd.o:main.cpp:(.text+0x42): undefined reference to `dlopen(char const*, int)'
collect2: ld returned 1 exit status


Exactly the same error (..\include contains dlfcn.h and ..\lib contains libdl.a). It's getting late, but tomorrow I'll try linking against a different static library to see if the problem is exclusive to win32-dlfcn or not.

I would try compiling and linking in separate commands (g++ -c for compiling, ld for linking), but I know that linking will fail due to the requirement of several standard static libraries that G++ automatically links (it's a pain to list them all).
Last edited on
Topic archived. No new replies allowed.