clinuxgcclinkeropen62541

Multible definitions including open source library (with seemingly correct linker settings?)


Im working on a program for my studies, that uses an open source library. It is meant to run on Raspberry Pi (Raspbian Kernel). Because of my intention to be also able to load it on a PLC i used mostly pure C. The Library itself comes with suitable header and .c files.

When i use the pre installed GCC compiler on the Raspberry my program compiles without any errors and works fine. Now here comes my problem:

I tried to get this projekt to work on windows using code::blocks IDE with MinGW installed. I revisited the library and downloaded the zip for windows (apperently same header and .c file, but also .lib and .dll included).

I set the search directories and linker settings within the project and included the header as usual with #include "header.h" for the relative path. It doesn't compile and gives alot multible definition and first defined here which usually indicate wrong linking and inclusion.

As i tried to identify some of this definitions i noticed that the functions which cause errors are defined one time in the library.c file. At the beginning of this .c file it also includes the header one time.

Short summary:

This works with raspbian GCC:

$ gcc -sdt=c99 main.c library.c -o executable

but gives errors with Windows IDE + MinGW

Am i missing something serious? The dynamic link lib should only be used by the executable afterwards. I thought maybe the libraby.c gets replaced by the library.lib but if i remove one of them the project doesn't know the functions. I also searched for wrong inclusions. I'm really at the end of my knowledge here, and also searched for posts that would help me, but those were mostly "where is the linker path" or "inclusion of .c files". It seems so an simple problem which i overlooked.

Any help Would be appriciated. I will supply more details if needed. Thanks!

Edit (2):

obj\Debug\open62541.o:open62541.c:(.text+0x3152a): undefined reference to `__imp_shutdown'
obj\Debug\open62541.o:open62541.c:(.text+0x3153f): undefined reference to `__imp_closesocket'
obj\Debug\open62541.o:open62541.c:(.text+0x315a7): undefined reference to `__imp_send'
obj\Debug\open62541.o:open62541.c:(.text+0x315b9): undefined reference to `__imp_WSAGetLastError'
.....

Edit (3)

Answer 1! Compiled good now, thanks everyone.


Solution

  • It looks like your library is open62541.

    There are two ways to include the library in your source:

    You are combining both methods on mingw which adds the whole library two times. Probably you only want to link the .c file without the .lib, thus your compile command should look something like this:

    gcc -sdt=c99 main.c open62541.c -o test
    

    Additionally, since open62541 needs the ws2_32 library on windows, the compiler should be called with:

    gcc -std=c99 main.c open62541.c -o test -lws2_32