NetBSD-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Custom pet projects compiling for NetBSD n00bs



Lord Vader <lvd.mhm%gmail.com@localhost> writes:

> Then, when I try to compile my code simply as gcc -c filename.c filename.o,
> it fails at #include <png.h>.
> Further examination of gcc with `gcc -xc -E -v -` shows it doesn't have
> default include path that point into /usr/pkg/include
>
> I managed to add them via -I or via CPATH env var, then I've managed to do
> the same for linking via -L or LIBRARY_PATH, then I even got the
> executable. Which again was unable to find .so under the /ust/pkg/lib ,
> unless I also use LD_LIBRARY_PATH for running it.

The basic issue is that there is a default linker search path, and that
does not include /usr/pkg/lib.  You found -I/usr/pkg/include for
headers, and -L/usr/pkg/include to find the library at link time, but
the run-time link path must also be stored in the executable.

The old-fashioned syntax is -R/usr/pkg/include and the new syntax is
either

  -Wl,-R/usr/pkg/lib

  -Wl,-rpath/usr/pkg/lib

The -Wl says that what follows is a linker flag, because typically one
invokes the linker via the C compiler.

Generally, build systems look for and add flags for dependent packages,
and to be done portably this must accomodate linker flag syntax rules
for different platforms.   This leads to using something like autoconf
or cmake, or some even newer system.

Another piece of the puzzle is pkg-config, implemented in pkgsrc as
pkgconf, which allows querying the needed flags.

  $ pkg-config --cflags libpng
  -I/usr/pkg/include/libpng16 
  $ pkg-config --libs libpng
  -Wl,-R/usr/pkg/lib -L/usr/pkg/lib -lpng16 -lz 


Use "objdump -a" and look at the lines NEEDED and RPATH.

> I have experience with different linux distros, including ubuntu and gentoo
> where everything seems to work off-the-box (provided I `apt install` or
> `emerge` appropriate packages), but here I fail and probably I do smth
> terribly wrong.

Actually you are quite close.   On many GNU/Linux systems, the standard
approach is to put everything with a prefix of /usr.   I suspect that if
you installed something to some other prefix, and tried to use it, you
would find the same problem and solution.

Attachment: signature.asc
Description: PGP signature



Home | Main Index | Thread Index | Old Index