Compiling against libusb-dev on Ubuntu - c

I am trying to compile the example libusb.c provided by libusb package (if you dl the source code.)
It doesn't work to say the least.
#include <stdio.h>
#include <sys/types.h>
#include <libusb/libusb.h>
That causes it to fail, there is no libusb/libusb.h it's usb.h, so I change that. And it fails in new and innovative ways.
I've copied the file over, exactly, and named it example.c
I am using these commands and variations:
gcc -o example example.c -lusb -L /usr/lib/libusb.a
gcc -o example example.c -lusb -L /usr/lib/libusb.so
The errors I get when compiling are:
example.c:25: error: expected ‘)’ before ‘*’ token
example.c: In function ‘main’:
example.c:46: error: ‘libusb_device’ undeclared (first use in this function)
example.c:46: error: (Each undeclared identifier is reported only once
example.c:46: error: for each function it appears in.)
example.c:46: error: ‘devs’ undeclared (first use in this function)
Line 25: static void print_devs(libusb_device **devs)
Line 46: libusb_device **devs;
At first I followed a tutorial, and that failed to compile, in more or less the same ways, so I decided to just try the provided example, and that failed.
Can anyone help me out? Explain what I am doing wrong, cause I am lost on this one.

This is what I had to do on Debian. It should be at least similar in Ubuntu.
Install libusb-1.0-0-dev
Instead of:
#include <libusb/libusb.h>
do:
#include <libusb.h>
Compile with:
gcc example.c `pkg-config --libs --cflags libusb-1.0`

Just en explanation why your attempt to replace libusb/libusb.h with usb.h fails: usb.h is a header from linux-headers, not from libusb-dev. You need #include <libusb.h> instead.

Related

F_SEAL_SEAL undeclared, even when headers are included

I'm trying to use file sealing on Linux. Here's an example C program.
#define _GNU_SOURCE
#include <unistd.h>
#include <fcntl.h>
int main(void) {
(void)F_SEAL_SEAL;
}
You can build it using gcc -Wall -o ./linux_file_sealing linux_file_sealing.c or similar.
When I build it, I get an error about F_SEAL_SEAL.
gcc -Wall -o ./linux_file_sealing linux_file_sealing.c
linux_file_sealing.c: In function ‘main’:
linux_file_sealing.c:7:19: error: ‘F_SEAL_SEAL’ undeclared (first use in this function)
printf("%d\n",F_SEAL_SEAL);
^
linux_file_sealing.c:7:19: note: each undeclared identifier is reported only once for each function it appears in
I'm including unistd.h and fcntl.h, as per the man page... so what else should I be doing, and where is that described?
(The man pages just say that sealing is "Linux-specific", but give no further details. This is the reason for including the GNU_SOURCE define, which is how you get the other Linux-specific stuff, but for F_SEAL_SEAL it seems to make no difference.)
(Ubuntu 16.04 LTS, Linux 4.4.0-36)
You want
#include <linux/fcntl.h>
instead of
#include <fcntl.h>

STDERR_FILENO undeclared on ubuntu

I'm trying to compile an example stack trace displaying code. When I compile the test.c file with:
gcc -g -rdynamic ./test.c -o test
I get following error:
./test.c: In function ‘handler’:
./test.c:16: error: ‘STDERR_FILENO’ undeclared (first use in this function)
./test.c:16: error: (Each undeclared identifier is reported only once
./test.c:16: error: for each function it appears in.)
My includes are the same as in the original post code:
#include <stdio.h>
#include <execinfo.h>
#include <signal.h>
#include <stdlib.h>
My machine is ubuntu 13.04. Am I missing some library or haven't included something?
Also #include <unistd.h>.
See this GNU documentation.

How to compile an example SDL program written in C?

I'm getting started with SDL and C programming. I have experience with other programming languages, but linking/compiling libraries in C is new to me. I am using Mac 10.8 and have installed latest stable 2.0 using the instructions in the read me (./configure; make; make install). Here is the sample code that I am trying to compile:
#include <stdlib.h>
#include <stdio.h>
#include "SDL.h"
int main(void)
{
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0) {
fprintf(stderr, "\nUnable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
atexit(SDL_Quit);
return 0;
}
When I try to compile my script using gcc example.c, I get an error:
example.c:3:17: error: SDL.h: No such file or directory
example.c: In function ‘main’:
example.c:7: error: ‘SDL_INIT_VIDEO’ undeclared (first use in this function)
example.c:7: error: (Each undeclared identifier is reported only once
example.c:7: error: for each function it appears in.)
example.c:7: error: ‘SDL_INIT_TIMER’ undeclared (first use in this function)
example.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
example.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
example.c:11: error: ‘SDL_Quit’ undeclared (first use in this function)
I tried searching the wiki, and tutorials, and any kind of documentation that I could find, but I could not find any example anywhere that showed how to properly compile a C program that uses SDL.
What do I need to do to compile this program?
A general hint for C beginners: read error logs top-down: often fixing first error will resolve all other. In your case first error is:
example.c:3:17: error: SDL.h: No such file or directory
As others have said, you need to instruct gcc where to find SDL.h. You can do this by providing -I option.
To check where SDL.h is installed by default I would issue
./configure --help
in the directory where you did build libsdl. Then look for --prefix, under Linux default prefix is often /usr/local. To compile your example I would issue (on Linux):
gcc example.c -I/usr/local/include
But the above command compiles and links the code. After successful compilation, gcc would throw another bunch of errors, one of them being undefined reference.
To prevent that, full command line to build your example (on Linux at least) would be:
gcc example.c -I/usr/local/include -L/usr/local/lib -lSDL
Where:
-I points compiler to directory with SDL.h,
-L points linker to directory with libSDL.a (or libSDL.so),
-l instructs linker to link with library, in our case libSDL.a or libSDL.so. Note that the lib prefix and .a/.so suffix is missing.
Please note that I didn't check this instruction, even on Linux machine (on the other hand I have no access to Mac OS machine).
One more thing: by default binary with the compiled and linked example will be called a.out. To change that you can provide -o option to gcc.
I found out you can use a tool called pkg-config to find out the compiler flags expected for a specific library.
$ pkg-config --cflags --libs sdl2
-D_THREAD_SAFE -I/usr/local/include/SDL2 -I/usr/X11R6/include -L/usr/local/lib -lSDL2
$ gcc example.c $(pkg-config --cflags --libs sdl2)
If you are using a Makefile, you need to prefix the command with shell:
all:
gcc example.c $(shell pkg-config --cflags --libs sdl2)
tl;dr
sudo apt install libsdl1.2-dev
You are missing the SDL library files. Just instal them and everything should work out of the box.
Different versions ans extensions
There are multiple versions of SDL, make sure you install the one that is required by your application. There are also additional libraries (called projects) for SDL that you also need to install if you use their features. For example if you use TTF fonts or image related functionalities. Just press TAB twice after you typed in sudo apt install libsdl to see all the available packages.

Using GraphicsMagick C API, Unknown Type Image Compile Error

I'm trying to use GraphicsMagick and got odd build errors, so I added #include <magick/magick.h> to
#include <stdio.h>
int main(int argc, char *argv[]){
printf("hello magick");
return 0;
}
just to see WTF was going on. Obviously hello_world compiles fine. Simply adding the magick header causes tons of errors compiling with any of the following:
clang or gcc -o test.o $(pkg-config --cflags --libs GraphicsMagick) test.c
clang or gcc -o test.o $(GraphicsMagick-config --cflags --libs) test.c
From clang:
zsh/2 1791 % clang -o test.o $(pkg-config --cflags --libs GraphicsMagick) test.c
In file included from test.c:2:
/usr/include/GraphicsMagick/magick/magick.h:19:9: error: unknown type name 'Image'
typedef Image
^
/usr/include/GraphicsMagick/magick/magick.h:20:28: error: unknown type name 'ImageInfo'
*(*DecoderHandler)(const ImageInfo *,ExceptionInfo *);
The solution suggested by Mr. Hale (#1) works perfectly for the test. Trying in the real project; gcc spits thousands of line of errors like:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.1/include/f16cintrin.h: In function ‘__m128i mm256_cvtps_ph(__m256, int)’: /usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.1/include/f16cintrin.h:73:66: error: cannot convert ‘__m256 {aka float}’ to ‘__vector(8) float’ for argument ‘1’ to ‘__vector(8) short int __builtin_ia32_vcvtps2ph256(__vector(8) float, int)’ return (__m128i) __builtin_ia32_vcvtps2ph256 ((__v8sf) __A, __I);
Since the only change from having the project build successfully and the above was uncommenting either one or both of the following:
#include <magick/api.h>
#include <magick/magick.h>
I'm quite sure I've got something wrong with the build settings. I'm not having success finding documentation on what particular restrictions GraphicsMagick places on compiler/linker options. Finding that might well solve the problem.
Use the <magick/api.h> header; this ensures types and forward declarations appear in the correct order.
Changing from using std=c++0x to std=gnu++11 in the project-wide CXXFLAGS seems to have resolved the issue. For whatever reason, it seems that graphicsmagick 1.3.18-3 is not usable from the c/c++ APIs with std=c++0x. I know this is not a complete explanation or answer, but it makes things build.
This has been fixed in GraphicsMagick 1.3.20.
I have found this in the ChangeLog:
2014-06-15 Bob Friesenhahn
wand/magick_compat.h: Use MAGICK_ATTRIBUTE definition from magick/common.h.
magick/common.h (MAGICK_ATTRIBUTE): Don't undefine __attribute__ since this may be used >by system or compiler headers. Define private macro instead. Resolves SourceForge bug #270 "Compile error with g++ -std=c++11".
RHEL/Fedora/CentOS users, check GraphicsMagick update request for EPEL7 in RedHat Bugzilla, Bug ID 1131926

error: unknown type name ‘bool’

I downloaded the source code and wanted to compile the file of scanner. It produces this error:
[meepo#localhost cs143-pp1]$ gcc -o lex.yy.o lex.yy.c -ll
In file included from scanner.l:15:0:
scanner.h:59:5: error: unknown type name ‘bool’
In file included from scanner.l:16:0:
utility.h:64:38: error: unknown type name ‘bool’
utility.h:74:1: error: unknown type name ‘bool’
In file included from scanner.l:17:0:
errors.h:16:18: fatal error: string: No such file or directory
compilation terminated.
And I tried to use different complier to compile it, but it appeared different errors.
[meepo#localhost cs143-pp1]$ g++ -o scan lex.yy.c -ll
/usr/bin/ld: cannot find -ll
collect2: ld returned 1 exit status
My os is 3.0-ARCH, I don't know why this happened. How do I fix the error?
C90 does not support the boolean data type.
C99 does include it with this include:
#include <stdbool.h>
C99 does, if you have
#include <stdbool.h>
If the compiler does not support C99, you can define it yourself:
// file : myboolean.h
#ifndef MYBOOLEAN_H
#define MYBOOLEAN_H
#define false 0
#define true 1
typedef int bool; // or #define bool int
#endif
(but note that this definition changes ABI for bool type so linking against external libraries which were compiled with properly defined bool may cause hard-to-diagnose runtime errors).
Just add the following:
#define __USE_C99_MATH
#include <stdbool.h>
Somewhere in your code there is a line #include <string>. This by itself tells you that the program is written in C++. So using g++ is better than gcc.
For the missing library: you should look around in the file system if you can find a file called libl.so. Use the locate command, try /usr/lib, /usr/local/lib, /opt/flex/lib, or use the brute-force find / | grep /libl.
Once you have found the file, you have to add the directory to the compiler command line, for example:
g++ -o scan lex.yy.c -L/opt/flex/lib -ll

Resources