1

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.

Andy.S.
  • 13
  • 4
  • 2
    When you say "gives errors", what do you mean by that? *What* errors? Please copy the full error output, complete and as text, and then paste it into the question body, without modification of the text. – Some programmer dude Aug 03 '17 at 08:02
  • "*multible definition*" <- hmm, try copy&paste? This would also add some context. What @Someprogrammerdude said. You can cut down on your explanative text, but please include all the details that are **really** relevant (like e.g. the verbatim output of commands you run) –  Aug 03 '17 at 08:04
  • Hm, this seems wrong somehow. If you have ``*.c`` and ``*.h`` files for the library, you should not need any ``*.lib`` anymore. The ``*.lib`` is usually a (precompiled) object you add - if you have the ``*.c``, you can actually build the library yourself (and that is what you apparently did on the raspi). – bipson Aug 03 '17 at 08:17
  • ok thanks sor far, ill check on creating the library without using the precompiled *.lib, maybe thats where i went wrong – Andy.S. Aug 03 '17 at 08:28
  • 1
    @Andy.S. it seems you need winsock functions. Add `-lws2_32`. – keltar Aug 03 '17 at 11:22
  • Thanks! that solved part 2 of my problem! im good to go now! – Andy.S. Aug 03 '17 at 11:41
  • @Andy.S. I know that you have to wait until you can answer your own question, due to reputation. To keep question and answer separated, it's better if you then split it up and remove the answer from the question. – Stefan Profanter Aug 03 '17 at 11:50
  • I updated my answer to also include ws2_32 – Stefan Profanter Aug 03 '17 at 11:51
  • ok will do, was the first time i posted here. i will replace it with edit 1 and 2 to keept the question histoy ok? – Andy.S. Aug 03 '17 at 11:57

1 Answers1

0

It looks like your library is open62541.

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

  • Build a shared/static lib and link it to your code
  • Enable Amalgamation which generates a single .c and .h file which you can directly compile with your code

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
Stefan Profanter
  • 6,458
  • 6
  • 41
  • 73
  • Thanks for the answer. I indeed use open62541 and tried to build a static `*.a` and linked it to the project without the open62541.c. This time it doesnt throw errors about the doubled definitions, but some about missing references. see edit 2 at the question. – Andy.S. Aug 03 '17 at 11:11
  • same errors like in edit (2) when im compiling from cmd with `gcc -std=c99 main.c open62541.c -o test` – Andy.S. Aug 03 '17 at 11:25
  • But it answers my initial question! no more miltiplied definitions. Thanks – Andy.S. Aug 03 '17 at 11:38