0

I am a C noob, so this may be a stupid question. I am trying to compile a .so file (shared library, if I have my terminology correct) of C objects (.o files), for the express purpose of importing them into Python via ctypes. I first compiled the *.so with

gcc -shared -o libvARAM.so ARAM.o ARAM_io.o io.o pre.o rule.o stat.o ART.o vARAM.o

This worked, except that when I tried loading via ctypes I was rewarded with:

OSError: ./libvARAM.so: undefined symbol: max

After some digging, I realized that max is not a standard C function. Calling ldd libvARAM.so informed me that one of the dependencies is libc.so.6. I created a symlink libc.so to libc.so.6 and then tried recompiling my .so as

gcc -shared -o libvARAM.so ARAM.o ARAM_io.o io.o pre.o rule.o stat.o ART.o vARAM.o -llibc

which generated

/usr/bin/ld: cannot find -llibc

The same error is generated if I also try -L/lib/i386-linux-gnu/ -llibc. I am aware of this thread, but feel it is not relevant to my situation, as the solution there is for a makefile. I am using Xubuntu if this matters.

Any help is sincerely appreciated!

Community
  • 1
  • 1
learner
  • 1,895
  • 2
  • 19
  • 21
  • When compiling some GNU software, the portability library included (compiled) is `libiberty.a`. This is included in the compilation with the option `-liberty`, of course. (Sad that they're subtracting liberty, but that's the convention for options, of course.) – Jonathan Leffler Sep 01 '12 at 18:26

2 Answers2

2

One: Linker flags don't work like that. For libXYZ.so, the corresponding linker flag is NOT -llibXYZ but only -lXYZ.

Two: even this is not needed, as the C standard library (-lc) is automatically linked to the executable.

Three: your problem most likely is that there should be a max() macro (as opposed to a function) defined in one of the header files, but you don't include this header file, so the compiler doesn't know it's a macro and treats it as a function - thenof course it can't find it in libc.so because it's not there.

  • Is it normal to include header files in .so files? I thought the header files were used to compile .c files into .o files. – learner Sep 01 '12 at 17:03
  • @learner (facepalm truncated) no. To compile c files to o files, you use a *complier*. **compiler != header file**. Two, one does not include header files to the .so file, but into c or other h files. –  Sep 01 '12 at 17:06
  • To clarify - I meant to ask if header files were used in the compilation from .c to .o. You are right - `max` (and `min`) are defined as macros in a separate header file. However, this header file **was** included in the compilation process. Not entirely sure what to do at this point... – learner Sep 01 '12 at 17:16
  • 1
    @learner If the header in which `max` is defined is included in all files where `max` is used, the compiler won't see `max` at all, and the linker won't search for it. The most probable cause of the problem is that you forgot to include the header in one file. – Daniel Fischer Sep 01 '12 at 17:32
  • I think you both were right in that the files using `max` and `min` were not directly importing the header file. I recompiled all files to include the header file `port.h` which defines `max` and `min`. However, I am still getting an error message, but this time `min` is the undefined symbol. What am I doing wrong? – learner Sep 01 '12 at 17:40
  • @learner: You should be getting a warning from your compiler that the function `max` (or `min`) is not declared before it is used if the header defining the macro is missing. If you aren't getting that warning, you need to find out how to turn on more compilation warnings because they'll prevent you making mistakes. When you say 'recompiled all files to include the header file `port.h`', do you mean you edited the files, added the line `#include "port.h"` and then recompiled? That's what you should have done. – Jonathan Leffler Sep 01 '12 at 18:22
  • @JonathanLeffler That is exactly what I did. – learner Sep 01 '12 at 19:40
  • 2
    @learner: OK, but you must have one of three problems if `min` is reported as undefined. Either (1) you missed editing `port.h` into one of the files, or (2) you missed recompiling a source file, or (3) the macro `min()` is not always defined by the `port.h` header. For (3), you need to review the contents of the header carefully. Frankly, (1) or (2) is more likely — we've all made similar mistakes on occasion. – Jonathan Leffler Sep 01 '12 at 22:16
  • 1
    Thanks! I posted an explanation of what specifically was wrong below, but you all were right - there were issues with that header file. Compiling with `-Wall` was what indicated that the header file wasn't being read correctly. – learner Sep 02 '12 at 19:55
  • @learner I'm glad you finally managed to solve it, and thanks for the accept! Yes, from now on promise me you'll be compiling all your projects using -Wall! ;-) –  Sep 02 '12 at 19:56
  • Absolutely! Would've saved a ton of time, too! – learner Sep 02 '12 at 19:57
0

Posting this so that if other newbies see it, they'll see the answer... Basically, my header file was as follows:

#ifdef UNIX
#define min(x, y) (x<y ? x : y)
#define max(x, y) (x<y ? y : x)
#endif

Removing the first and last lines (i.e. no ifdef check) allowed the compiler to read these definitions. Apparently it had not been reading them.

learner
  • 1,895
  • 2
  • 19
  • 21