The correct way to 'build' github packages

Programming, for all ages and all languages.
Post Reply
sunnysideup
Member
Member
Posts: 106
Joined: Sat Feb 08, 2020 11:11 am
Libera.chat IRC: sunnysideup

The correct way to 'build' github packages

Post by sunnysideup »

You can consider me a Linux newbie here. I use Manjaro Linux, and `uname -r` gives me `5.4.44-1-MANJARO`

To install new packages, I use `pacman`, and it's extremely hassle-free and seems well abstracted, and almost seems like using `setup.exe` in windows. So far so good.

However, I've come to realize that 'the best way (often)' to install packages is via compilation from source, where the source is present in someplace like github.

In my case, I wanted to install `libcpuid` from source. A search gives me two links: https://github.com/anrieff/libcpuid and http://libcpuid.sourceforge.net/index.html.

I follow the instructions on the repo:

Code: Select all

libtoolize
autoreconf --install
./configure
make
This is successful, but I still can't use the headers and the library. So I try `make install`. This installs the library (both libcpuid.so and libcpuid.a) in `/usr/local/lib` and also installs the headers in `/usr/local/include/libcpuid`. I've read that `/usr/local` is the default PREFIX for packages that are built and installed from source... So far so good.

I want to try out a simple program: http://libcpuid.sourceforge.net/documentation.html. I compile as they suggest: `gcc -o example example.c $(pkg-config libcpuid --cflags --libs)`. This gives me my first error:

Code: Select all

Package libcpuid was not found in the pkg-config search path.
Perhaps you should add the directory containing `libcpuid.pc'
to the PKG_CONFIG_PATH environment variable
Package 'libcpuid', required by 'virtual:world', not found
I find a `libcpuid.pc` in the source directory (I'm sure this isn't the 'right' way at all - Adding the git cloned directory to the environment variable? I don't think so) and add the source directory to PKG_CONFIG_PATH, and compile again. It compiles (`pkg-config libcpuid --cflags --libs` gives me `-I/usr/local/include/libcpuid -L/usr/local/lib -lcpuid`)
, but when I execute, I get this error:

Code: Select all

error while loading shared libraries: libcpuid.so.15: cannot open shared object file: No such file or directory
I'm not sure how to proceed. I am also sure that compilation from source isn't meant to be this difficult, and perhaps I'm complicating things too much. What is the right way to compile packages from source? Moreover, Why is it right that the final compilation commands looks like `gcc -o example example.c -I/usr/local/include/libcpuid -L/usr/local/lib -lcpuid`? I thought that since we install the header and library in a standard system library, we don't need to specify the full path of the header?
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: The correct way to 'build' github packages

Post by Korona »

'make install' should install the .pc file to a directory where pkg-config should be able to find it (usually /usr/local/lib/pkg-config?).

To avoid the .so not found issue, you can use RPATHs. Build systems like Meson will automatically add the appropriate RPATH to your binary. You can also set LD_LIBRARY_PATH to the directory that contains the library but that's more hacky.
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
linuxyne
Member
Member
Posts: 211
Joined: Sat Jul 02, 2016 7:02 am

Re: The correct way to 'build' github packages

Post by linuxyne »

The headers are installed inside /usr/local/include/libcpuid directory, and not directly inside /usr/local/include.
(When compiling the sample, add -v to the gcc commandline to see the default locations where the compiler searches for headers.)

In the sample/example, change

Code: Select all

#include <libcpuid.h>
to

Code: Select all

#include <libcpuid/libcpuid.h>
This should allow you to compile the sample with just:

Code: Select all

gcc sample.c -lcpuid
Upon execution, the error about libcpuid.so.15 is perhaps related to the way /usr/local directory is treated during runtime linking.

See [1], [2] and [3] below for some info about that treatment, in the context of
Arch/Manjaro.

See [2] for a small patch, which is a superset of a solution given inside [1].
The topic [1]'s solution: create a new file (as root), say usrlocal.conf, inside the /etc/ld.so.conf.d/ directory,
with the contents:

Code: Select all

/usr/local/lib
Then, run (again, as root), the command 'ldconfig'.
Now the sample executable should be able to pull the .so file.

[1] https://bbs.archlinux.org/viewtopic.php?id=99807
[2] https://bugs.archlinux.org/task/20059?p ... pened=2263
[3] https://forum.manjaro.org/t/building-fr ... al/81490/5
Post Reply