undefined reference to `libusb_xxx' - c

I'm using Ubuntu 22.04 and CLion to run this in C:
#include <stdio.h>
#include <sys/types.h>
#include <libusb-1.0/libusb.h>
static void print_devs(libusb_device **devs)
{
libusb_device *dev;
int i = 0;
while ((dev = devs[i++]) != NULL) {
struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0) {
fprintf(stderr, "failed to get device descriptor");
return;
}
printf("%04x:%04x (bus %d, device %d)\n",
desc.idVendor, desc.idProduct,
libusb_get_bus_number(dev), libusb_get_device_address(dev));
}
}
int main(void)
{
printf("hello");
libusb_device **devs;
int r;
ssize_t cnt;
r = libusb_init(NULL);
if (r < 0)
return r;
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0)
return (int) cnt;
print_devs(devs);
libusb_free_device_list(devs, 1);
libusb_exit(NULL);
return 0;
}
It's a test.c for libusb. However, with libus-dev and libusb-1.0.0-dev both downloaded, I kept got this error:
====================[ Build | usbTool | Debug-System ]==========================
/snap/clion/219/bin/cmake/linux/bin/cmake --build /home/wonster/CLionProjects/usb-tool/cmake-build-debug-system --target usbTool -- -j 6
[ 50%] Building C object CMakeFiles/usbTool.dir/test.c.o
[100%] Linking C executable usbTool
/usr/bin/ld: CMakeFiles/usbTool.dir/test.c.o: in function `print_devs':
/home/wonster/CLionProjects/usb-tool/test.c:12: undefined reference to `libusb_get_device_descriptor'
/usr/bin/ld: /home/wonster/CLionProjects/usb-tool/test.c:20: undefined reference to `libusb_get_device_address'
/usr/bin/ld: /home/wonster/CLionProjects/usb-tool/test.c:20: undefined reference to `libusb_get_bus_number'
/usr/bin/ld: CMakeFiles/usbTool.dir/test.c.o: in function `main':
/home/wonster/CLionProjects/usb-tool/test.c:31: undefined reference to `libusb_init'
/usr/bin/ld: /home/wonster/CLionProjects/usb-tool/test.c:35: undefined reference to `libusb_get_device_list'
/usr/bin/ld: /home/wonster/CLionProjects/usb-tool/test.c:40: undefined reference to `libusb_free_device_list'
/usr/bin/ld: /home/wonster/CLionProjects/usb-tool/test.c:42: undefined reference to `libusb_exit'
collect2: error: ld returned 1 exit status
gmake[3]: *** [CMakeFiles/usbTool.dir/build.make:97: usbTool] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:83: CMakeFiles/usbTool.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:90: CMakeFiles/usbTool.dir/rule] Error 2
gmake: *** [Makefile:124: usbTool] Error 2
I just don't know the reason and how to solve this. I've followed a lot of blogs on Internet but not helpful.
I'm guessing whether there's any wrong with the CMake file:
cmake_minimum_required(VERSION 3.22)
project(usbTool C)
set(CMAKE_C_STANDARD 99)
#声明头文件路径
#set(INC_DIR /opt/homebrew/Cellar/libusb/1.0.26/include/libusb-1.0)//绝对路径
set(INC_DIR ./include)
#声明链接库路径
#set(LINK_DIR /opt/homebrew/Cellar/libusb/1.0.26/lib)//绝对路径
set(LINK_DIR ./lib)
#引入头文件
include_directories(${INC_DIR})
#引入库文件
link_directories(${LINK_DIR})
add_executable(usbTool test.c)
#引入第三方库
target_link_libraries(usbTool libusb-1.0.a)
And this is the project content:
Any help is remarkable! Thanks in advance!

Related

Failed to compile with secp256k1 library

So I was following this tutorial: https://nickfarrow.com/Cryptography-in-Bitcoin-with-C/ I installed libsecp256k1 from https://www.howtoinstall.me/ubuntu/18-04/libsecp256k1-dev/ but while compiling my program:
#include <secp256k1.h>
#include <stdio.h>
static secp256k1_context *ctx = NULL;
int main()
{
ctx = secp256k1_context_create(
SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
/* Declare the private variable as a 32 byte unsigned char */
unsigned char seckey[32];
/* Load private key (seckey) from random bytes */
FILE *frand = fopen("/dev/urandom", "r");
/* Read 32 bytes from frand */
fread(seckey, 32, 1, frand);
/* Close the file */
fclose(frand);
/* Loop through and print each byte of the private key, */
printf("Private Key: ");
for (int i = 0; i < 32; i++)
{
printf("%02X", seckey[i]);
}
printf("\n");
}
i get:
josh#pc:~/Code$ gcc prvkey.c -o exec
/tmp/cc5OVPMJ.o: In function `main':
prvkey.c:(.text+0x1d): undefined reference to `secp256k1_context_create'
collect2: error: ld returned 1 exit status
Thanks in advance!
Try:
gcc prvkey.c -o exec -lcrypto -lsecp256k1
gcc -l links with a library file.
Let me know if that works or any questions let me know.

trouble with the ncurses library

i am trying to get continuous keyboard input in c and have tried following the answer found here. however, i am getting undefined reference errors when using ncurses functions.
my code:
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <ncurses.h>
typedef struct
{
int fps;
int width;
int height;
int frame;
} window;
typedef struct
{
int x;
int y;
} position;
typedef struct
{
position pos;
} snake;
int kbhit(void)
{
int ch = getch();
if (ch != ERR)
{
ungetch(ch);
return 1;
}
else
{
return 0;
}
}
void draw(window win, snake s)
{
for (int i = -1; i <= win.height; i++)
{
for (int j = -1; j <= win.width; j++)
{
if ((i == -1 || i == win.height) || (j == -1 || j == win.width))
{
printf("#");
}
else if (j == s.pos.x && i == s.pos.y)
{
printf("O");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
int main()
{
printf("Welcome to the Snake Game\n");
sleep(3);
window win = {1, 20, 10, 0};
snake s = {{19, 9}};
int key_code;
initscr();
cbreak();
noecho();
nodelay(stdscr, TRUE);
scrollok(stdscr, TRUE);
while (true)
{
printf("\e[1;1H\e[2J");
printf("%d\n", win.frame);
draw(win, s);
if (kbhit())
{
key_code = getch();
printf("%d", key_code);
}
usleep((int)((1.0 / win.fps) * 1000) * 1000);
win.frame++;
}
return 0;
}
output:
/usr/bin/ld: /tmp/ccZ2eIK1.o: in function `kbhit':
game.c:(.text+0xf): undefined reference to `stdscr'
/usr/bin/ld: game.c:(.text+0x17): undefined reference to `wgetch'
/usr/bin/ld: game.c:(.text+0x2a): undefined reference to `ungetch'
/usr/bin/ld: /tmp/ccZ2eIK1.o: in function `main':
game.c:(.text+0x136): undefined reference to `initscr'
/usr/bin/ld: game.c:(.text+0x13b): undefined reference to `cbreak'
/usr/bin/ld: game.c:(.text+0x140): undefined reference to `noecho'
/usr/bin/ld: game.c:(.text+0x147): undefined reference to `stdscr'
/usr/bin/ld: game.c:(.text+0x154): undefined reference to `nodelay'
/usr/bin/ld: game.c:(.text+0x15b): undefined reference to `stdscr'
/usr/bin/ld: game.c:(.text+0x168): undefined reference to `scrollok'
/usr/bin/ld: game.c:(.text+0x1b6): undefined reference to `stdscr'
/usr/bin/ld: game.c:(.text+0x1be): undefined reference to `wgetch'
collect2: error: ld returned 1 exit status
You must include the NCURSES library with the link. A portable way to do this is like this:
$ gcc -o game game.c $( pkg-config --cflags --libs mcurses )
Or just include the "-lncurses" library as mentioned previously.
On an RPM-based system, you'll need the ncurses-devel package installed on the build machine.

driver in globus toolkit

I have been trying to run an example driver program given in the globus toolkit website. This is the program:
#include "globus_xio.h"
int main(int argc,char *argv[])
{
globus_result_t res;
char * driver_name;
globus_xio_driver_t driver;
globus_xio_stack_t stack;
globus_xio_handle_t handle;
globus_size_t nbytes;
char * contact_string = NULL;
char buf[256];
contact_string = argv[1];
driver_name = argv[2];
globus_module_activate(GLOBUS_XIO_MODULE);
res = globus_xio_driver_load(driver_name,&driver);
assert(res == GLOBUS_SUCCESS);
res = globus_xio_stack_init(&stack, NULL);
assert(res == GLOBUS_SUCCESS);
res = globus_xio_stack_push_driver(stack, driver);
assert(res == GLOBUS_SUCCESS);
res = globus_xio_handle_create(&handle, stack);
assert(res == GLOBUS_SUCCESS);
res = globus_xio_open(handle, contact_string, NULL);
assert(res == GLOBUS_SUCCESS);
do
{
res = globus_xio_read(handle, buf, sizeof(buf) - 1, 1, &nbytes, NULL);
if(nbytes > 0)
{
buf[nbytes] = '\0';
fprintf(stderr, "%s", buf);
}
} while(res == GLOBUS_SUCCESS);
globus_xio_close(handle, NULL);
globus_module_deactivate(GLOBUS_XIO_MODULE);
return 0;
}
When I compile this using the command
cc -I /usr/include/globus globus_xio_example.c
I get the following errors
/tmp/ccLMLlIi.o: In function `main':
globus_xio_example.c:(.text+0x57): undefined reference to `globus_i_xio_module'
globus_xio_example.c:(.text+0x5c): undefined reference to `globus_module_activate'
globus_xio_example.c:(.text+0x75): undefined reference to `globus_xio_driver_load'
globus_xio_example.c:(.text+0xb1): undefined reference to `globus_xio_stack_init'
globus_xio_example.c:(.text+0xf2): undefined reference to `globus_xio_stack_push_driver'
globus_xio_example.c:(.text+0x133): undefined reference to `globus_xio_handle_create'
globus_xio_example.c:(.text+0x179): undefined reference to `globus_xio_open'
globus_xio_example.c:(.text+0x1d1): undefined reference to `globus_xio_read'
globus_xio_example.c:(.text+0x22b): undefined reference to `globus_xio_close'
globus_xio_example.c:(.text+0x230): undefined reference to `globus_i_xio_module'
globus_xio_example.c:(.text+0x235): undefined reference to `globus_module_deactivate'
collect2: ld returned 1 exit status
It appears that the command used to compile is insufficient:
cc -I /usr/include/globus globus_xio_example.c
Specifically, the linker process indicates that there are a number of symbols that could not be resolved. I suspect that the command used to compile is lacking directives as to which library(s) needs to be linked to resolve the undefined symbols.
HINT:
Use 'globus-makefile-header' to help determine library dependencies.
% globus-makefile-header -flavor=gcc32dbg globus_xio > header
Examine the contents of 'header' for appropriate makefile macros.
Include header in your makefile and use the needed makefile macros.
Try this gcc -I /usr/include/globus globus_xio_example.c -lglobus_xio -lglobus_common

libtomcrypt usage benchmarking

I wanted to compare AES algorithm performance from libtomcrypt in Windows and Ubuntu by creating a benchmark-like file, but I have got errors while coding it. Please help me. Below is my file for comparing:
Compare.c:
`#include <time.h> `
#include <tomcrypt.h>
#define MIN_TIME 10.0
#define MIN_ITERS 20 `
double test_rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) {
int iterations = 0;
clock_t start;
double elapsed=0.0;
int out;
start=clock();
do{
out = rijndael_ecb_encrypt(pt, ct, skey);
iterations++;
elapsed=(clock()-start)/(double)CLOCKS_PER_SEC;
} while(elapsed<MIN_TIME || iterations<MIN_ITERS);
elapsed=1000.0*elapsed/iterations;
printf("%s \n",pt);
printf("%s \n",skey->data);
printf("%s \n",ct);
printf("iterations: %8d \n",iterations);
printf("%8.2lf ms per iteration \n",elapsed);
printf("out: %d \n",out);
return elapsed;
}
int main() {
unsigned char pt[22]="-K4)<i50~'APg2fa7DiV";
unsigned char ct[22];
unsigned char key[16]="EDB1C6D13FC72";
symmetric_key *skey;
int err;
double tout1;
printf("%x",sizeof(pt));
printf("%l",sizeof(key));
if((err=rijndael_setup(key,16,0,skey))!=CRYPT_OK) {
printf("%s",error_to_string(err));
return -1;
}
tout1=test_rijndael_ecb_encrypt(pt,ct,skey);
printf("%s \n",ct);
printf("%f",tout1);
return 0;
}
But when I compile this it shows runtime errors as:
gcc -o "TestC" ./src/TestC.o
./src/TestC.o: In function `test_rijndael_ecb_encrypt':
/home/anvesh/workspace/TestC/Debug/../src/TestC.c:27: undefined reference to `rijndael_ecb_encrypt'
./src/TestC.o: In function `test_rijndael_ecb_decrypt':
/home/anvesh/workspace/TestC/Debug/../src/TestC.c:53: undefined reference to `rijndael_ecb_decrypt'
./src/TestC.o: In function `main':
/home/anvesh/workspace/TestC/Debug/../src/TestC.c:82: undefined reference to `rijndael_setup'
/home/anvesh/workspace/TestC/Debug/../src/TestC.c:83: undefined reference to `error_to_string'
collect2: error: ld returned 1 exit status
make: *** [TestC] Error 1
Where did I go wrong?
You forgot to link with tomcrypt library. Compile with -ltomcrypt to link the library:
gcc file.c -ltomcrypt

ffmpeg code does not link (undefined reference to avcodec_register_all

I am trying to compile a simple introductory program using ffmpeg that tries to check if the mp3 codec is available. While the code compiles OK, I am facing difficulty in solving linker errors. Here is the code:
#include <stdio.h>
#include <math.h>
#include <libavcodec/avcodec.h>
float *generateSinusoid(unsigned int sampleRate, unsigned int nSecondsAudio) {
unsigned int nsamples = (nSecondsAudio * sampleRate);
float *arr;
arr = (float*) malloc(sizeof(float) * nsamples);
int i = 0;
for(i = 0; i < nsamples; i++) {
arr[i] = 20 * sin(2.f * (M_PI) * (330/sampleRate) * i); /*frequency of 330H
z*/
}
return arr;
}
int main(int argc, char *argv[]) {
avcodec_register_all();
AVCodec *codec;
unsigned int sampleRate = 22050; /*assumed.*/
unsigned int nSecondsAudio = 4;
float *arr;
arr = (float *) malloc(sizeof(float) * nSecondsAudio * sampleRate);
/*Step 1. Generate sinusoid.*/
arr = generateSinusoid(sampleRate, nSecondsAudio);
/* Step 2. See if encoder exists.*/
/*codec = avcodec_find_encoder(AV_CODEC_ID_MP3);*/
if(!codec) { /*codec = NULL.*/
printf("MP3 codec not found!!!!");
} else {
printf("MP3 codec found!!!");
}
return 0;
}
The code is compiled and linked like so:
encoding_mp3: encoding_mp3.o
gcc encoding_mp3.o -o encoding_mp3 -L/cygdrive/c/Users/Desktop/webserver/cygnus/lib/w32api -L/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/ffmpeg_dev/lib -lm -luser32 -lpthread -lavcodec
encoding_mp3.o: encoding_mp3.c
gcc -I/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/ffmpeg_dev/include -I/cygdrive/c/Users/Desktop/webserver/cygnus/usr/include -g -c encoding_mp3.c -o encoding_mp3.o
clean:
rm encoding_mp3.o encoding_mp3
Linking gives the following error:
gcc -I/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/ffmpeg_dev/include -I/cygdrive/c/Users/Desktop/webserver/cygnus/usr/include -g -c encoding_mp3.c -o encoding_mp3.o
gcc encoding_mp3.o -o encoding_mp3 -L/cygdrive/c/Users/Desktop/webserver/cygnus/lib/w32api -L/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/ffmpeg_dev/lib -lm -luser32 -lpthread -lavcodec
encoding_mp3.o: In function `main':
/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/work/encoding_mp3.c:31: undefined reference to `_avcodec_register_all'
collect2: ld returned 1 exit status
make: *** [encoding_mp3] Error 1
I have gone through most of the threads on SO regarding this problem and here is what I have tried so far:
- Put libraries at the end of all non-option arguments
- Commented out code that references functions. This seems to work. The undefined reference errors go away after all function calls are removed, though the presence of a struct AVCodec does not cause any problems.
Any help on this is most welcome.

Resources