# ldconfig works but not ld



## codeape (Feb 25, 2010)

Hello, my first post

I run FreeBSD 8 with the latest and gratest (done freebsd-update fetch and install plus portmaster -a). It is newly installed 

I discovered my problem after installing devel/libevent. After installing libevent I added it to my configure.ac in my little project. After doing all autotools steps i discovered that libevent could not be found with a AC_CHECK_LIB.

I did: 

```
ldconfig -r | grep libevent 
107:-levent-1.4.3 => /usr/local/lib/libevent-1.4.so.3
108:-levent_core-1.4.3 => /usr/local/lib/libevent_core-1.4.so.3
109:-levent_extra-1.4.3 => /usr/local/lib/libevent_extra-1.4.so.3
```

and after that I did: 

```
ld -l event
ld: cannot find -levent
```

and after that i tested more libs in /usr/local/lib with `ldconfig -r | grep` and `ld -l` and every lib was found with ldconfig but none with ld. All libs in /usr/lib could be found though with both ldconfig and ld, like this:

```
ld -l ncurses
ld: warning: cannot find entry symbol _start; not setting start address
/lib/libc.so.7: undefined reference to `environ'
/lib/libc.so.7: undefined reference to `__progname'
```

Last thing i did was:

```
ld --verbose -l event
GNU ld version 2.15 [FreeBSD] 2004-05-23
  Supported emulations:
   elf_i386_fbsd
   elf_x86_64_fbsd
using internal linker script:
==================================================
/* Script for -z combreloc: combine and sort reloc sections */
OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64",
	      "elf64-x86-64")
OUTPUT_ARCH(i386:x86-64)
ENTRY(_start)
SEARCH_DIR("/lib"); SEARCH_DIR("/usr/lib");
/* Do we need any of these for elf?
   __DYNAMIC = 0;    */
SECTIONS
{
  /* Read-only sections, merged into text segment: */
  PROVIDE (__executable_start = 0x400000); . = 0x400000 + SIZEOF_HEADERS;
  .interp         : { *(.interp) }
  .hash           : { *(.hash) }
  .dynsym         : { *(.dynsym) }
  .dynstr         : { *(.dynstr) }
  .gnu.version    : { *(.gnu.version) }
  .gnu.version_d  : { *(.gnu.version_d) }
  .gnu.version_r  : { *(.gnu.version_r) }
  .rel.dyn        :
    {
      *(.rel.init)
      *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)
      *(.rel.fini)
      *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)
      *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)
      *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)
      *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)
      *(.rel.ctors)
      *(.rel.dtors)
      *(.rel.got)
      *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)
    }
  .rela.dyn       :
    {
      *(.rela.init)
      *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)
      *(.rela.fini)
      *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)
      *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)
      *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)
      *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)
      *(.rela.ctors)
      *(.rela.dtors)
      *(.rela.got)
      *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)
    }
  .rel.plt        : { *(.rel.plt) }
  .rela.plt       : { *(.rela.plt) }
  .init           :
  {
    KEEP (*(.init))
  } =0x90909090
  .plt            : { *(.plt) }
  .text           :
  {
    *(.text .stub .text.* .gnu.linkonce.t.*)
    /* .gnu.warning sections are handled specially by elf32.em.  */
    *(.gnu.warning)
  } =0x90909090
  .fini           :
  {
    KEEP (*(.fini))
  } =0x90909090
  PROVIDE (__etext = .);
  PROVIDE (_etext = .);
  PROVIDE (etext = .);
  .rodata         : { *(.rodata .rodata.* .gnu.linkonce.r.*) }
  .rodata1        : { *(.rodata1) }
  .eh_frame_hdr : { *(.eh_frame_hdr) }
  /* Adjust the address for the data segment.  We want to adjust up to
     the same address within the page on the next page up.  */
  . = ALIGN (0x100000) - ((0x100000 - .) & (0x100000 - 1)); . = DATA_SEGMENT_ALIGN (0x100000, 0x1000);
  /* Ensure the __preinit_array_start label is properly aligned.  We
     could instead move the label definition inside the section, but
     the linker would then create the section even if it turns out to
     be empty, which isn't pretty.  */
  . = ALIGN(64 / 8);
  PROVIDE (__preinit_array_start = .);
  .preinit_array     : { *(.preinit_array) }
  PROVIDE (__preinit_array_end = .);
  PROVIDE (__init_array_start = .);
  .init_array     : { *(.init_array) }
  PROVIDE (__init_array_end = .);
  PROVIDE (__fini_array_start = .);
  .fini_array     : { *(.fini_array) }
  PROVIDE (__fini_array_end = .);
  .data           :
  {
    *(.data .data.* .gnu.linkonce.d.*)
    SORT(CONSTRUCTORS)
  }
  .data1          : { *(.data1) }
  .tdata	  : { *(.tdata .tdata.* .gnu.linkonce.td.*) }
  .tbss		  : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }
  .eh_frame       : { KEEP (*(.eh_frame)) }
  .gcc_except_table   : { *(.gcc_except_table) }
  .dynamic        : { *(.dynamic) }
  .ctors          :
  {
    /* gcc uses crtbegin.o to find the start of
       the constructors, so we make sure it is
       first.  Because this is a wildcard, it
       doesn't matter if the user does not
       actually link against crtbegin.o; the
       linker won't look for a file to match a
       wildcard.  The wildcard also means that it
       doesn't matter which directory crtbegin.o
       is in.  */
    KEEP (*crtbegin*.o(.ctors))
    /* We don't want to include the .ctor section from
       from the crtend.o file until after the sorted ctors.
       The .ctor section from the crtend file contains the
       end of ctors marker and it must be last */
    KEEP (*(EXCLUDE_FILE (*crtend*.o ) .ctors))
    KEEP (*(SORT(.ctors.*)))
    KEEP (*(.ctors))
  }
  .dtors          :
  {
    KEEP (*crtbegin*.o(.dtors))
    KEEP (*(EXCLUDE_FILE (*crtend*.o ) .dtors))
    KEEP (*(SORT(.dtors.*)))
    KEEP (*(.dtors))
  }
  .jcr            : { KEEP (*(.jcr)) }
  .got            : { *(.got.plt) *(.got) }
  _edata = .;
  PROVIDE (edata = .);
  __bss_start = .;
  .bss            :
  {
   *(.dynbss)
   *(.bss .bss.* .gnu.linkonce.b.*)
   *(COMMON)
   /* Align here to ensure that the .bss section occupies space up to
      _end.  Align after .bss to ensure correct alignment even if the
      .bss section disappears because there are no input sections.  */
   . = ALIGN(64 / 8);
  }
  . = ALIGN(64 / 8);
  _end = .;
  PROVIDE (end = .);
  . = DATA_SEGMENT_END (.);
  /* Stabs debugging sections.  */
  .stab          0 : { *(.stab) }
  .stabstr       0 : { *(.stabstr) }
  .stab.excl     0 : { *(.stab.excl) }
  .stab.exclstr  0 : { *(.stab.exclstr) }
  .stab.index    0 : { *(.stab.index) }
  .stab.indexstr 0 : { *(.stab.indexstr) }
  .comment       0 : { *(.comment) }
  /* DWARF debug sections.
     Symbols in the DWARF debugging sections are relative to the beginning
     of the section so we begin them at 0.  */
  /* DWARF 1 */
  .debug          0 : { *(.debug) }
  .line           0 : { *(.line) }
  /* GNU DWARF 1 extensions */
  .debug_srcinfo  0 : { *(.debug_srcinfo) }
  .debug_sfnames  0 : { *(.debug_sfnames) }
  /* DWARF 1.1 and DWARF 2 */
  .debug_aranges  0 : { *(.debug_aranges) }
  .debug_pubnames 0 : { *(.debug_pubnames) }
  /* DWARF 2 */
  .debug_info     0 : { *(.debug_info .gnu.linkonce.wi.*) }
  .debug_abbrev   0 : { *(.debug_abbrev) }
  .debug_line     0 : { *(.debug_line) }
  .debug_frame    0 : { *(.debug_frame) }
  .debug_str      0 : { *(.debug_str) }
  .debug_loc      0 : { *(.debug_loc) }
  .debug_macinfo  0 : { *(.debug_macinfo) }
  /* SGI/MIPS DWARF 2 extensions */
  .debug_weaknames 0 : { *(.debug_weaknames) }
  .debug_funcnames 0 : { *(.debug_funcnames) }
  .debug_typenames 0 : { *(.debug_typenames) }
  .debug_varnames  0 : { *(.debug_varnames) }
  /DISCARD/ : { *(.note.GNU-stack) }
}


==================================================
attempt to open /usr/bin/../libdata/libevent.so failed
attempt to open /usr/bin/../libdata/libevent.a failed
attempt to open /lib/libevent.so failed
attempt to open /lib/libevent.a failed
attempt to open /usr/lib/libevent.so failed
attempt to open /usr/lib/libevent.a failed
ld: cannot find -levent
```
To me it looks like /usr/local/lib is not even searched.

What is going on?


----------



## DutchDaemon (Feb 25, 2010)

Format your posts, please.


----------



## SirDice (Feb 26, 2010)

Add *-L/usr/local/lib* to the ld command. See ld(1).


----------



## codeape (Feb 26, 2010)

SirDice said:
			
		

> Add *-L/usr/local/lib* to the ld command. See ld(1).



Thnx for the reply and yes that works good for now:

```
$ export LDFLAGS=-L/usr/local/lib
$ ./configure
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for sctp_bindx in netinet/sctp.h ... yes
checking for event_init in -levent... yes
checking for initscr in -lncurses... yes
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... ./install-sh -c -d
checking for gawk... no
checking for mawk... no
checking for nawk... nawk
checking whether make sets $(MAKE)... yes
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking for gcc... (cached) gcc
checking whether we are using the GNU C compiler... (cached) yes
checking whether gcc accepts -g... (cached) yes
checking for gcc option to accept ISO C89... (cached) none needed
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking dependency style of g++... gcc3
checking for a BSD-compatible install... /usr/bin/install -c
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: executing depfiles commands
$
```

However, this looks like a ld bug to me.


----------



## SirDice (Feb 26, 2010)

codeape said:
			
		

> However, this looks like a ld bug to me.


It isn't.


----------



## codeape (Feb 26, 2010)

SirDice said:
			
		

> It isn't.



I have done `ldconfig -m /lib/local/lib` and can see libevent with libconf and ld can still not find libevent? How can that not be a bug. Explain please :stud


----------



## SirDice (Feb 26, 2010)

codeape said:
			
		

> I have done ldconfig -m /lib/local/lib and can see libevent with libconf and ld can still not find libevent? How can that not be a bug. Explain please :stud



ld and the runtime linker are two separate entities that have nothing to do with each other.


----------



## codeape (Feb 26, 2010)

SirDice said:
			
		

> ld and the runtime linker are two separate entities that have nothing to do with each other.



I know the difference of the ld command and ld.so however it looks like there is a a difference how these work in different *nixes. In linux for example ldconfig creates its hints in /etc/ld.so.conf and that file is used by both the ld command and ld.so. I am new to FreeBSD so I do not know all the details and it looks like /var/run/ld-elf.so.hints is used instead. 

I do not know, but it looks like you can not configer the ld command in FreeBSD the same way you can do it in the linux dists I have been using.


----------



## achix (Feb 26, 2010)

What's the output of `# ldconfig -r | head -2` in your system?
FreeBSD's ldconfig prepares the hints file and there is no special need for a configuration file (e.g. /etc/ldâ€elf.so.conf)

Anyways, back to your original issue: from ld(1)


> -Lsearchdir
> --library-path=searchdir
> Add path searchdir to the list of paths that ld will search for arâ€
> chive  libraries  and  ld control scripts.  You may use this option
> ...



I doubt the ld(1) will know about e.g. /usr/local/lib/qt4, etc.. so you gotta specify this by -L


----------



## codeape (Feb 26, 2010)

achix said:
			
		

> What's the output of `# ldconfig -r | head -2` in your system?



Here you are:


```
/var/run/ld-elf.so.hints:
	search directories: /lib:/usr/lib:/usr/lib/compat:/usr/local/lib
```



			
				achix said:
			
		

> FreeBSD's ldconfig prepares the hints file and there is no special need for a configuration file (e.g. /etc/ldâ€elf.so.conf)
> 
> Anyways, back to your original issue: from ld(1)
> 
> ...



Ok, thanks


----------



## codeape (Feb 28, 2010)

After some research, I have one more question. An evolved solution to what you guys are suggesting is to add CFLAGS and LDFLAGS to my configure.ac like this:


```
CFLAGS="$CFLAGS -I/usr/local/include"
LDFLAGS="$LDFLAGS -L/usr/local/lib"
```

This article suggest that:
http://www.gentoo.org/doc/en/articles/autotools-practices.xml

Is that FreeBSDish? The article suggests that.


----------

