0

So i'm trying to create .so files for use in the ctypes library for python but am running into a bit of a snag.

The files in the directory are cbw.h, cbw64.lib, and Main.c

During my attempt of creating the .so file using the following command string in Windows Powershell:

gcc -shared -o testlib.so -fPIC Main.C -lcbw

I get the error 'cannot find -lcbw'.

I put this in, because if I don't, I get that there are 'undefined references' in Main.C which were defined during the 'include cbw.h' line in Main.c

What is wrong with my command? I am able to create a .so file if I use a simple program like Hello world but cannot do so as soon as I try to import predefined functions.

edit:

If i use the object library cbw64.lib I get the error, ' error adding symbols: file in wrong format'

alk
  • 69,737
  • 10
  • 105
  • 255
user2852630
  • 39
  • 1
  • 8
  • It can't find it, which means it's not in any of the places it's looking. gcc uses the `-L` command to add additional library search paths, something like `-L/dir/where/cbw/lives` – yano Feb 08 '19 at 21:05
  • @yano , So, I took your advice and changed it such that: `gcc -shared -o testlib.so -fPIC Main.C -L C:/user/myname/desktop/FolderContainingCbw.h -lcbw ` and It still gives me the cannot find -lcbw error – user2852630 Feb 08 '19 at 21:10
  • 2
    `-L` specifies a directory where your object code library resides. If you're trying to use `-l` and `-L` to "link in" a .h header file that's the wrong usage. Header files are `#include`d in code. The order that arguments are supplied to gcc when linking also matters, whose rules I can't remember off the top of my head. This might be helpful: https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc – yano Feb 08 '19 at 21:13
  • @yano , right and I do have that header file included in the main.c code. I'm following the advice that was given on this post (because its a very similar problem) which is why I'm trying to link it in powershell. [link](https://stackoverflow.com/questions/22426574/gcc-undefined-reference-to) – user2852630 Feb 08 '19 at 21:18
  • 1
    right, the accepted answer from your link shows exactly what you need to do, and shows the correct order for the arguments. Use the `-L` option to specify where the cbw _library_ is. In linux, they typically have `.a` extensions, in windows it's typically `.lib`. `-L` doesn't care about where Cbw.h is, it cares about where the executable binary code for the function you're trying to call is located. Cbw.h is a header file, not a library. I can't speak to any Windows-specific quirks with Powershell, but I wouldn't expect there to be any. – yano Feb 08 '19 at 21:26
  • @yano, I did as instructed. The result I get is ` ./C/cbw64.lib : error adding symbols: File in wrong format ` and `collect2.exe: error: ld returned 1 exit status` .... Any chance you're familiar with that error? – user2852630 Feb 08 '19 at 21:43
  • yeah that's something entirely different. Sounds like the the library was compiled with a different [ABI](https://en.wikipedia.org/wiki/Application_binary_interface) than your compiler is using. Tough to suggest a fix without knowing more. The provider of the library should be able to give you more info. Looks like the library is 64-bit, perhaps you're compiling for 32-bit? I haven't used gcc on windows much, not too familiar unfortunately. If you have access to the source code you could compile the library yourself. – yano Feb 08 '19 at 21:52
  • 1
    The error means `gcc` doesn't understand the contents of `cbw64.lib`. How was it made? – jxh Feb 08 '19 at 21:52
  • 1
    `gcc -m64`? What is your toolchain that contains *gcc*? *MinGW32*? Also the library name doesn't quite qualify to be used with `-l`: instead use `gcc -shared -o testlib.so -fPIC Main.C "Path/to/cbw64.lib"`. How was *cbw64.lib* created? – CristiFati Feb 08 '19 at 21:59
  • So to everyone asking how cbw64.lib was created: I'm unsure. I acquired these functions and related files from the vendor of a certain microcontroller. – user2852630 Feb 09 '19 at 00:20
  • the `cbw64.lib` is for a 64 bit architecture. From the error message, is seems your architecture is actually 32 bit, so you should be including the `cbw.lib` library instead – user3629249 Feb 09 '19 at 00:47

0 Answers0