How to check C libraries installed without root access? - c

I am working on a school project in a limited environment (archlinux) where I don't have root access. The subject says that I am allowed to use all libraries that are already installed. I am coding in C using gcc.
How to get a list of all those libraries ?

For those libraries managed by the pkg-config utility, the following command will show all installed libraries:
pkg-config --list-all | less
However, not all libraries are so managed so you may be forced to go through the /usr/lib and /usr/local/lib directories.

As noted, not all libraries necessarily use pkg-config. Given that this is ArchLinux, as a fallback you could learn to use the package manager, to list the installed packages. That is called pacman.
Anything that is installed as such on ArchLinux would be part of a package.
The query options can show you all of the files installed for given packages:
To list all files for a given package, use pacman -Qlpackage_name
To list all packages, use pacman -Q
(scripting that, to list all ".so" files which are installed, by package name would be an interesting exercise).

ls /lib /usr/lib /usr/local/lib

Related

How to use C library installed with vcpkg on linux?

I'm trying to install libwebsockets C library with vcpkg according to the instruction. And don't understand something.
OS - Ubuntu 20.04
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg install [library-name]
Library is installed and what to do next? How to compile test files with the library?
The question is more about how to use vcpkg on linux.
You can give an example of another library installed with vcpkg.
vcpkg is a C/C++ package manager, it is very necessary in windows.
However, in ubuntu, itself provides a very complete package management mechanism.
Therefore, even if you are building a cross platform software system, do not use vcpkg in ubuntu :)
You can try this:
$> sudo apt install libwebsockets-dev
In this way, the libwebsockets header files and library files you need have been installed and can be used directly.
Here is another example:
I want to install gtkmm4 in ubuntu 20.04 LTS, since gtkmm4 is not available for apt download I'm installing it with vcpkg.
for simplification, I'm setting VCPKG_DIR to the vcpkg directory I cloned.
export VCPKG_DIR=/path/to/vckpg
Then for a C++ program, you can write CMake file like below
PROJECT(gtkmmtest)
cmake_minimum_required(VERSION 3.10)
set(VCPKG_DIR $ENV{VCPKG_DIR})
include(${VCPKG_DIR}/scripts/buildsystems/vcpkg.cmake) # --> important
# Use the package PkgConfig to detect GTK+ headers/library files
FIND_PACKAGE(PkgConfig REQUIRED)
FIND_PACKAGE(Threads REQUIRED)
pkg_check_modules(GTK4 REQUIRED gtk4)
PKG_CHECK_MODULES(GTKMM gtkmm-4.0)
include_directories(${GTK4_INCLUDE_DIRS})
include_directories(${GTKMM_INCLUDE_DIRS})
link_directories(${VCPKG_DIR}/packages/gtk_x64-linux/lib)
link_directories(${GTK4_LIBRARY_DIRS})
add_definitions(${GTK4_CFLAGS_OTHER})
target_link_libraries(${GTKMMTEST} PRIVATE ${GTK4_LIBRARIES} ${GTKMM_LIBRARIES} pthread)
Complete CMake file can be found here
You can still use the the standard include_directories and link_directories if there is no PkgConfig avilable.
eg:
include_directories(${VCPKG_DIR}/packages/gtkmm_x64-linux/include/gtkmm-4.0/)
Library is installed and what to do next? How to compile test files with the library?
The question is more about how to use vcpkg on linux.
The answer to this question really depends on your buildsystem and the port/library you want to use and not the platform itself.
In the case of libwebsockets libwebsockets-config.cmake get installed so you could use CMake and do a find_package(libwebsockets CONFIG REQUIRED) to get the imported targets the port exports within LibwebsocketsTargets.cmake. Of course this requires setting CMAKE_TOOLCHAIN_FILE to the vcpkg toolchain (<vcpkg_root>/scripts/buildsystems/vcpkg.cmake) or including it before the first project() in your CMakeLists.txt (more details are mentioned in the vcpkg docs which you hopefully read....)
Other libraries/ports might export *.pc files. For these FindPkgConfig.cmake can be used directly (see CMake docs) or you can setup PKG_CONFIG_PATH and prepend <vcpkg_root>/installed/<triplet (here probably: x64-linux)>/(debug/)lib/pkgconfig for other buildsystems like autotools or manual makefiles etc.
In the end how to use vcpkg or more precisly the libraries from it depends on what buildsystem you intend to use.

Where is my "stdio.h" in Mac?

I know that Mac OS X is a Unix-based system. And I heard that C standard library such as stdio.h, is located in /usr/local/include or /usr/include. But there is not any kind of library in this directory. I use Terminal to search this directory and I also use command like find ./ -iname "stdio.h", but nothing comes out. However, strangely enough, gcc -test.c -o test command works out. How did it happen? I want to know where my C library is located.
p.s I also use Xcode, too. Is it related with this application? help me!
And I have AWS EC2 linux server, and it has both libraries that i refereed above.
If you have Xcode but have not installed the optional Command Line Tools package then the standard includes and libraries may not be found in the usual place. Try:
$ find /Applications/Xcode.app -name stdio.h
and you'll probably see something like:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/c++/4.2.1/tr1/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/sys/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/c++/4.2.1/tr1/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/sys/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/c++/4.2.1/tr1/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/sys/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/c++/4.2.1/tr1/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/stdio.h
However you might want to install the Command Line Tools package if you plan on doing any non-Xcode (i.e. command line) programming. You will then see the usual headers and libraries in /usr/include and /usr/lib.
If you don't have Command Line Tools installed you can run:
xcode-select --install
A dialogue box opens for you to accept the license agreement and so on.
(This was missing from the above responses.)
create/update a symlink for /usr/include to have the libs detected:
sudo ln -sf /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include /usr/include
The above path can be found by searching for stdio.h
find /Applications/Xcode.app -path '*/usr/include/stdio.h'
I have to do this with every XCode/MacOS SDK update, Faced this today with the XCode 7 upgrade.
Once the command line tools are installed, they cannot be re-installed with xcode-select, so the path may not be updated with a Mac AppStore upgrade.
Uninstalling & Reinstalling XCode and then running xcode-select --install might update the path, but is an overkill.
Some posts also mention xcode-select --switch /Application/Xcode.app, but I didn't have much luck with it.
The root reason is missing the /usr/include folder, installing command-line tools sometimes will not automatically add it.
Install the package at
/Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
In my laptop it appears in many locations like /usr/include/stdio.h and /usr/include/sys/stdio.h and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/stdio.h.
If you already built your locate database, you can use
locate stdio.h
If you haven't already, build it. The locate command is awesome!
Due to Apple's new System Integrity Protection (SIP), you will run into this error when attempting the solution of creating/updating a symlink to /usr/include
ln: /usr/include: Operation not permitted
Instead of disabling SIP, the better way to do it is to create symlinks in /usr/local/include instead, e.g.
ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/* /usr/local/include/
This works for people who have already installed CommandLineTools and was unable to find stdio.h inside /usr/include
In the folder
Applications/Xcode/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include
or similar.
I have installed "CommandLineTools", and the "stdio.h" file exist in both Xcode and CommandLineTools diretory.
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/stdio.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/stdio.h
This is my path for C/C++ library in M1 Mac 2022
/Library/Developer/CommandLineTools/usr/include/c++/v1

library path for Tcl extension in C

I use Tcl_CreateObjCommand to build a Tcl extension in C called libA.so, the C code call the function from an external C library called libext.so, where should I put libext.so, so that the tcl could find and load it when it load libA.so
Use a package installer
If you have a package manager you should install tcl/tk with it.
sudo apt-get install tk
or you might want also tk-dev (which is sometimes called tk-devel):
sudo apt-get install tk tk-dev
Install manually
You can put so's manually in /usr/local (libraraies in /usr/local/lib/ and include files in /usr/local/include/), But remember to avoid installing them under /usr if you do have a packaging system.
There's also an option of putting them under your project directory, but you'll have to help the dynamic linker find them (using LD_LIBRARY_PATH or ld.so.conf). Also, you'll have to update your project's include paths and library paths.

GNU GSL BLAS library, undefined symbols

Is there anyone that can give me a simple run down of how to install the GSL library so that it'll work with XCode (5.1) on a Mac (Mavericks, 10.9)? I keep getting this error:
Undefined symbols for architecture x86_64:
"_gsl_matrix_add", referenced from:
_myProject in main.o
I initially installed GSL 1.16 in /Library/gsl-1.16
I did the whole ./configure, make, and make install
THINGS I HAVE TRIED TO FIX THIS:
1) Going to "Build Settings" in XCode and adding "/Library/gsl-1.16"
to both the library and header search paths
2) Creating a symbolic link from the ...usr/include path (... being the SDK (MacOSX 10.9) location XCode is using) to the Library path.
3) Adding "-lgsl" to the "Arguments passed on Launch" section of my project's scheme
4) Adding "-Wall -I/Library/include" to the "Arguments passed on Launch" section of my project's scheme
Nothing has worked. Xcode reports that it can find the headers, but it can't reference the methods associated with the header files. Truly interesting. I have no idea, I've done everything I can think of and Google.
From your description it is difficult to locate whether you set the build settings correctly. I installed GSL using macports and not sure how it would be like if installed directly from source. But I believe you need to figure out where the make install installs the GSL library and header to, since typically they are installed into different paths, which are not the path you run make install.
And since typically make install will install headers to /usr/local/include and libraries to /usr/local/lib, I guess it is possible that you set the wrong path to the libraries. Assume you figured out that your GSL library is in /usr/local/lib (and typically they should), you just need to do one of the two things (not both):
Change Library search path to /usr/local/lib. (and header search path to /usr/local/include as you did)
Change compiler arguments to -Wall -I/usr/local/include -L/usr/local/lib. (last two arguments start with capital i and capital L).
Here is the procedure how I install GSL using macports and set up it in Xcode to run a matrix addition:
Install macports if you haven't done it. Refer to https://www.macports.org/install.php .
In terminal, type sudo port install gsl and have several cups of coffee.
By default, macports installs librarys to /opt/local/lib and headers to /opt/local/include. So after you create a C++ project in Xcode, go to build settings, add the three settings below:
Library search paths: /opt/local/lib
Header search paths: /opt/local/include
Other linker flags: -lgsl (depending on what you want to do, you may also need to add -lgsl -lgslcblas and other flags.)
Now it is up and running. Include <gsl/gsl_matrix.h> and gsl_matrix is ready to use.

What is the right way to install header files from some package?

I want to build some package from source (e2fsprogs to be more concrete) and install its header files to my system. After that I will delete the build tree so it will not be accessible anymore. What is the right way to do this?
When I want to install program, I make simply:
$ ./configure
$ make
# make install
What are the equivalent actions when I want to install headers?
For e2fsprogs, quoting verbatim from the INSTALL file shipped with the sources:
7) Install the include files and libraries
You can run `make install-libs' to install the include files and
libraries. Please note that this installation is not needed for the
programs to work. It is only needed if you expect to develop other
programs using the libraries or if you want to compile other program
using these libraries (like the 4.4BSD dump and restore port).
More generally though, when I want to find out what is the 'proper' way to install something that has worked for others, I look at:
What the package looks like, in my favourite distro
How the package is built for my favourite distro

Resources