The Place to Start for Operating System Developers
It is currently Thu Mar 04, 2021 7:49 pm

All times are UTC - 6 hours

Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: The correct way to 'build' github packages
PostPosted: Thu Jun 25, 2020 10:19 pm 

Joined: Sat Feb 08, 2020 11:11 am
Posts: 105
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:
autoreconf --install

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:

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:

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?

 Post subject: Re: The correct way to 'build' github packages
PostPosted: Fri Jun 26, 2020 12:01 am 

Joined: Thu May 17, 2007 1:27 pm
Posts: 824
'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].

 Post subject: Re: The correct way to 'build' github packages
PostPosted: Fri Jun 26, 2020 6:22 am 

Joined: Sat Jul 02, 2016 7:02 am
Posts: 148
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
#include <libcpuid.h>

#include <libcpuid/libcpuid.h>

This should allow you to compile the sample with just:
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

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:

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

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group