How to use the hidapi library from Signal11? - c

I installed the hidapi library from Signall11 on my windows10 pc (using minGW). But now I'm having some trouble actually getting it to work with gcc. I have some main.c file in which I include the hidapi.h file. My gcc command looks like
gcc main.c
I'm not sure where I'm going wrong because whenever I try to run this command I get an undefined reference error to some function that is defined in the hidapi.h file.

A full compile command for a project using hidapi is like this:
gcc -o your_app your_app.c -lhidapi-hidraw
It's not enough to include #include "hidapi.h" in the C-code, which does let gcc compile. You also need -lhidapi-hidraw to link with the library. I.e. compiling is in fact a 2 step process.

Related

ld does not find secondary shared objects using ld.so.conf.d at compile time in Manjaro-Linux

I stumbled over a weird behavior of ld (compile time linking) in Manjaro-Linux.
Assuming I want to compile test.c which is dependent on libprimary.so and libprimary.so is dependent on libsecondary.so, which are installed in non standard paths.
I could compile like this (both are working):
gcc test.c -L/path/to/primaryLibDir -lprimary -L/path/to/secondaryLibDir -lsecondary -o test
or like this:
gcc test.c -L/path/to/primaryLibDir -lprimary -Wl,-rpath-link=/path/to/secondaryLibDir -o test
A look into ld manual under the -rpath-link option is telling that if -rpath-link is not used and the not directly dependent library is not given with -lsecondary, the file is (among other methods with higher prioriry tried before), also searched using the paths in /etc/ld.so.conf (priority 7).
I tried this in my Manjaro-Linux dsitribution and in the Ubuntu WSL.
And it works in all of them when writing the path directly into /etc/ld.so.conf.
If I write the path into a /etc/ld.so.conf.d/test.conf file (which is included via include /etc/ld.so.conf.d/*.conf in /etc/ld.so.conf) it works only in the Ubuntu WSL. In Manjaro Linux, it does not find the library anymore then.
Can anyone imagine, why this is not working? I think it must have something to do with the Manjaro setup.

Compiling cmocka on windows

I'm trying to compile a simple unit test on my windows machine.
When I'm trying to compile my test I'm using the shared library flag.
gcc -c -L./bin/ -lcmocka .\Test.c .\src\some_module.c
gcc .\Test.o .\some_module.o -o main
But the second line throws this error:
undefined reference to `_cmocka_run_group_tests'
However, if I'm compiling using directly the cmocka.c file which I downloaded from their git it works fine:
gcc -c .\lib\cmocka.c .\Test.c .\src\some_module.c
gcc .\Test.o .\some_module.o .\cmocka.o
What am I doing wrong in the first compilation?
In addition, I would happy to understand the difference between the two compilations. Which one is the better practice?
Thank you
In order to compile your code, the compiler does not need to know where to look for the library. It's enough if the compiler "finds" the declarations of the functions which are usually in the header files provided by the library.
This step is done in the first line of your compilation procedure (maybe you need to specify the folder to the header files by adding -Ipath/to/headers/):
gcc -c .\Test.c .\src\some_module.c
The library itself is "combined" with your code during the linking step, which is done during your second compilation step. Here you need to specify the library (and its path via -Lpath/to/library, if the linker does not find the library on its own):
gcc .\Test.o .\some_module.o -o main -L./bin/ -lcmocka
You should definitely not use your second approach and compile the library by yourself.

Compiling c program with dependencies, h and h0 files

I am trying to compile the gjh solver - written in C - into an executable file in windows. It is available on netlib
I downloaded the c file and am using gcc compiler via WinGW on windows' command prompt. Trying to compile the gjh.c file directly gave me an error that says:
gjh.c:33:21: fatal error: getstub.h: No such file or directory
#include "getstub.h"
compilation terminated.
I assumed that compiling gjh.c requires the dependency getstub.h.
getstub.h is not the only dependency required, there are other dependencies, namely: arith.h, asl.h, funcadd.h, and stdio1.h. All of these files are available on the same link where I found getstub.h. However, arith.h0 and stdio1.h0 are available instead of arith.h and stdio1.h.
Are these files the same? I tried to rename the .h0 files to .h and tried to compile gjh.c, but I got this error:
collect2.exe: error: ld returned 1 exit status
Are the two files the same? If not, is there any way for me to compile the gjh solver successfully into an .exe?
If that's the only problem in compiling, try using the -I switch in gcc:
gcc -I/my/path/to/include/files -o gjh gjh.c
the -I switch hints to gcc where to find your #include files.
I am not sure about the stdio1.h. I think your approach to rename is OK but that reference to external functions such as Sprintf. You need to link with a library defining that. If you know where it comes from, use the -L and -l switch in gcc for that:
gcc -I/my/path/to/include/files -L/my/path/to/library -lnameoflibrary \
-o gjh gjh.c

Compiling the project using libuv with CMake

I have got a small async udp server - RiDE.
And i can't build it with CMake. When i trying to i get errors:
undefined reference to 'uv_default_loop'
undefined reference to 'uv_udp_init'
...(other libuv functions)
But when i build it with command like this:
gcc -std=c11 RiDE_server.h RiDE_server.cpp main.cpp -o main.x -Wall -luv
everything is ok.
I think that the problem is in src/CMakeLists.txt file, but i can't understand how to fix it. Path to libuv headers on my machine - /usr/include. Path to libuv shared libs - /usr/lib/x86-64-linux-gnu.
Run make VERBOSE=1. What linker command does it run? It must be missing -luv. To fix that, add something like this to your CMakeLists.txt:
target_link_libraries(foo uv)

GCC compiler is unable to find pcre.h

I am trying to compile a C program which uses regexes on FreeBSD. I have checked in /usr/local/include and the file pcre.h is definitely there.
However, no matter what I do, I get the following compiler error:
/usr/home/myname/project/include/pcre_wrap.h:4:18: error: pcre.h: No such file or directory
What could be going wrong? My understanding of C libraries on Unix could be better...
As the comment above says you need to use #include. If this isn't working you may want to export an environment variable C_INCLUDE_PATH that points to the header file.
Failing that why not try adding -I/usr/local/include to your gcc call, something like gcc myfile.c -I/usr/local/include -o myexe

Resources