9

I'm trying to compile the following minimal C code on ubuntu 10.10:

#include <sys/capability.h>

void main(void)
{
        cap_t cap;
        cap = cap_get_proc();
}

with gcc -lcap test.c which gives me the following error:

/tmp/ccCQFyXS.o: In function `main':
test.c:(.text+0x9): undefined reference to `cap_get_proc'
collect2: ld a retourné 1 code d'état d'exécution

I have libcap2-dev and libcap2-bin installed, version 2.21

ls /lib/libcap.*
/lib/libcap.a  /lib/libcap.so  /lib/libcap.so.2  /lib/libcap.so.2.21

The same code with same gcc command build successfully on arch and gentoo

What should I do to get it build on ubuntu too?

tomix86
  • 1,336
  • 2
  • 18
  • 29
kuroneko
  • 425
  • 2
  • 4
  • 10
  • FYI, your code builds fine on my 64-bit Ubuntu 10.10 box (with `libcap-dev` 2.19 installed). – NPE Dec 01 '11 at 08:34

3 Answers3

9

Try gcc -Wl,--no-as-needed -lcap test.c or gcc test.c -lcap.
Hope this helps!

another.anon.coward
  • 11,087
  • 1
  • 32
  • 38
  • Thank you very much! Both work fine, but why `gcc -lcap test.c` works fine for my gentoo and arch configs but not for the ubuntu one? – kuroneko Dec 01 '11 at 08:47
  • 1
    @kuroneko:I think to the linker `--as-needed` is option is being passed by default which links the library only when the symbols are encountered which are actaully used in the binary. This is done to avoid linking unnecessary libraries & improve start up time. See [this gentoo link](http://www.gentoo.org/proj/en/qa/asneeded.xml) about the use of this option. The way you were linking, the library was prior to the source thus was not being linked. Unfortunately I am not able to find the right links with more info about this. – another.anon.coward Dec 01 '11 at 09:02
3

I'm working on Ubuntu 10.04 and I had the same problem (sys/capability.h not present on the file system).

I resolved this problem installing via Synaptic Package Manager the package libcap-dev (version 1:2.17-2ubuntul) which populates the /usr/include/sys folder with the capability.h file.

SchmitzIT
  • 9,227
  • 9
  • 65
  • 92
Albi61
  • 31
  • 2
1

For RHEL:

yum install libcap-devel
gcc -lcap test.c
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
Amit Singh
  • 87
  • 4