How to link Libraries in non-standard locations for compilation - c

I'm new to linux, and am trying to compile and install some libraries.
Unfortunately, things are quite difficult as I am unable to obtain sudo access to my machine, and had to install the libraries in non-standard locations.
I'm having trouble getting the compiler to find the libraries I installed.
One of the libraries (https://github.com/tpm2-software/tpm2-tss/blob/master/INSTALL.md) I'm trying to install has dependencies on the other libraries, and I am getting the following error code when trying to compile, and am unable to fix it.
src/tss2-esys/esys_crypto_ossl.c:11:10: fatal error: openssl/evp.h: No such file or directory
#include <openssl/evp.h>
compilation terminated.
make[1]: *** [Makefile:14063: src/tss2-fapi/api/libtss2_fapi_la-Fapi_AuthorizePolicy.lo] Error 1
In file included from ./src/tss2-fapi/fapi_int.h:11, from src/tss2-fapi/api/Fapi_ChangeAuth.c:18:
./src/tss2-fapi/ifapi_policy_instantiate.h:13:10: fatal error: json-c/json.h: No such file or directory
#include <json-c/json.h>
compilation terminated.
So far, I've been trying to compile it in bash using the following commands:
./bootstrap
export PKG_CONFIG_PATH=/home/me/test/lib/pkgconfig:$PKG_CONFIG_PATH
export LDFLAGS='-L../missing_libs -lssl -L../missing_libs -lz -L../missing_libs/json-c/.libs -ljson-c -L../missing_libs/curl-7.68.0/lib/.libs -lcurl -L../openssl-1.0.2 -lcrypto'
export CFLAGS='-I../missing_libs/curl-7.68.0/include/lib:../missing_libs/json-c:../missing_libs/openssl-1.0.2'
./configure --prefix=/home/me/test --with-udevrulesdir=/lib/udev --disable-doxygen-doc
make -j$(nproc)
The LDFLAGS are the folders that contain my .so and .a files, while the CFLAGS are the folders that contain my h files.
Can I check if anyone knows what I am not linking properly?
Thanks!

I found the problem. Apparently I was linking to the wrong place.
Since library was searching for , I should not link to the directory where json.h is, but the directory where json-c/json.h is.
So for my CFLAGS should be "-I../missing_libs" instead of "-I../missing_libs/json-c"
Thanks for the help Jonathan!

Related

How to fix "cannot find -lz"

I am working on code have Zlib.h header, This header is found in my code folder, I compile this code by using
gcc -o x xx.c -lz
but I get on this
/usr/bin/ld: cannot find -lz
collect2: error: ld returned 1 exit status
This happen just with Linux that I installed in a VBox.
How to fix that.
Try installing 'zlib1g-dev'. On Ubuntu this following command will install the library.
sudo apt install zlib1g-dev
When you type gcc foo.c, you ask gcc to compile and link the given file.
1. Compilation
Compilation consist of transforming the source file into an object file.
This step need the included files, like zlib.h to be found by gcc.
This step seems to be correct on system.
NB: You can ask gcc to only do this step typing gcc -c foo.c, or better gcc -Wall -c foo.c
2. Link
Once the object files have be created, then need to be linked to create an executable file.
It's that step that failed for you: your linked can't find the already compiled functions needed by your code.
When linking with option -lz, you tell your linker "search for libz.so file to find the missing functions"
On current linux distribution, you can install package like libz-dev to install the .so file in well known places. (/lib, /usr/lib, /usr/local/lib...)
If you don't have the libz.so file installed on the library search path, you can specify where is the library to your linker.
For instance, if libz.so is if /bar/baz directory, you can type gcc foo.c /bar/baz/libz.so. The same for libz.a.
In any case, you'll need the libz.so file or at least the libz.a file
See also What's the difference between .so, .la and .a library files?

Linking libraries with a MinGW project in VS code

I am using MinGW version 4.5.2 on windows 10. I am trying to link multiple libraries to a project in VS code. When I try run this command: g++ main.c -o main.exe -I"C:\\Users\\USER\\Programming\\OPenGL Library\\glfw\\include\\glfw3.h" -L"C:\\Users\\USER\\Programming\\OpenGL Library\\glfw\\lib-mingw-w64\\". I get a compile error: main.c:2:24: fatal error: GLFW\glfw3.h: No such file or directory What is wrong with the command above that gives this linking error. As far as I can tell I have pointed MinGW to the library and specified the header file. I am not sure what else I need to add to the command for link the GLFW library as well as other libraries such as GLEW or glm.
If your include directive looks like
#include <GLFW/glfw3.h>
you need to supply a path to a directory which itself contains a directory named GLFW.

undefined reference to gzdopen, gzclose, gzread

I have compiled and installed the 2.2.7.2 version of libxml. While compiling, I have this error: Makefile:755: recipe for target 'install-data-local' failed. But the lib files are correctly generated (libxml2.a and libxml2.so).
I'd like to use libxml2 in a C project so I edited my makefiles in order to integrate lib files (libxml2.a and libxml2.so).
The problem is that when I compile my project I get the followings errors:
/libxml2.a(xmlIO.o): In function xmlGzfileRead': undefined reference togzdopen'
/libxml2.a(xmlIO.o): In function xmlGzfileRead': undefined reference togzclose'
/libxml2.a(xmlIO.o): In function xmlGzfileRead': undefined reference togzread'
...etc
It seems that I have to install the zlib library in order to resolve this linker errors. I installed the zlib library and edited the LD_LIBRARY_PATH in order to add the path where are the zlib libraries.
I recompiled my project, but I still always having the same linker errors.
Would you please help me to resolve those linker errors.
Regards.
See the libxml2 FAQ:
Troubles compiling or linking programs using libxml2
Usually the problem comes from the fact that the compiler doesn't get the right compilation or linking flags. There is a small shell script xml2-config which is installed as part of libxml2 usual install process which provides those flags. Use
xml2-config --cflags
to get the compilation flags and
xml2-config --libs
to get the linker flags. Usually this is done directly from the Makefile as:
CFLAGS=`xml2-config --cflags`
LIBS=`xml2-config --libs`
On my current system, the output from xml2-config --libs is
-lxml2 -lz -lpthread -licucore -lm

Having issues compiling an exploit using GCC

I was trying to compile a C exploit for a security class I'm in and was struggling to get GCC to perform. The issue is that my /usr/include folder is missing folders that GCC is looking for to handle the includes of the file. The first error below describes a folder that doesn't exist.
asm/page.h: No such file or directory
What I've tried so far:
Symlink it with my /usr/src/linux-headers-4.4.0-kali1-common/include/* folders, but files within that folder start throwing errors that they in turn can't find other files.
Using GCC's -I parameter to manually specify each folder to look in for my includes but this also doesn't work. (Below)
gcc 10613.c -o workdamnit-I/usr/src/linux-headers-4.4.0-kali1-common/include/asm-generic/ -I/usr/src/linux-headers-4.4.0-kali1-common/include/linux/ -I/usr/src/linux-headers-4.4.0-kali1-common/include/uapi/asm-generic/ -I/usr/src/linux-headers-4.4.0-kali1-common/include/uapi/linux/
ERROR: In file included from /usr/include/stdio.h:33:0,
from 10613.c:2:
/usr/src/linux-headers-4.4.0-kali1-common/include/linux/stddef.h:4:31: fatal error: uapi/linux/stddef.h: No such file or directory
compilation terminated.
I updated the import statement to use page.h from my kali linux common headers. When I tried to run this, I received the below error:
'PAGE_SIZE' undeclared (first use in this function).
Lastly, I tried to compile with wine gcc but this particular exploit uses a socket library that I guess can't be compiled on a windows machine.
GCC version: 5.3.1
Link to exploit: https://www.exploit-db.com/exploits/10613/
My knowledge of C and its compilation requirements is very limited. Any assistance would be greatly appreciated.
Please give a usable and compilable example: https://stackoverflow.com/help/mcve
Based on the errors, it looks like -I/usr/src/linux-headers-4.4.0-kali1-common/include/uapi/linux/ should actually probably be -I/usr/src/linux-headers-4.4.0-kali1-common/include.

Using pocketsphinx in c

I am trying to install and use pocketsphinx on ubuntu in c language but I keep getting this error
/Desktop/Programming/C/Tests$ gcc libraries.c -o libraries
libraries.c:2:26: fatal error: pocketsphinx.h: No such file or directory
#include <pocketsphinx.h>
^
compilation terminated.
I have followed the step required to install sphinxbase and pocketsphinx found here but I keep getting the error every time I compile my code. Is there something i missed?
How did you install pocketsphinx? and what do you mean by "installing"
To work with a library you first need to have the headers in place where your compiler can find them (for GCC for example by using the -I option) and then you need to have the library installed so your compiler can build against it.
Judging from the error you didn't specify the header files of the library to your compiler
You can add manually
First go to the includes folder
$ cd /usr/include
Then add the pocketsphinx.h manually
$ sudo ln -s /my/path/to/pocketsphinx.h
So then when you want to include pocketsphinx.h file, you can use:
#include <pocketsphinx.h>

Resources