Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5288

C/C++ • Re: Hard time getting started

$
0
0
I also came across a site that showed two parameters for gcc as -L/home/pi/cpp and -lbcm2835. I noticed that bcm2835 is not a directory anywhere. What do these two parameters mean?
"-L" adds a directory to the list of paths which will be searched. "-l" specifies the name (mostly) of the object library to look for. Thus..

Code:

$ gcc -o c c.c -L /wrk -lfoo
..will create an executable 'c' (-o c) by compiling "c.c". It will look for a library called "libfoo.a" and will also look in folder "/wrk/" for it. Similarly..

Code:

$ g++ -o c c.cpp -L . -lfoo
..will create an executable 'c' by compiling the C++ file "c.cpp". It will add the current working directory (-L .) to where it looks for "libfoo.a".

You'd expect to write "-llibfoo.a" but for historic reasons the "lib" prefix is automatically added. Also there are two kinds of object library - static (eg: libfoo.a) and shared (eg: libfoo.so). Very loosely, you specify neither the ".so" nor the ".a" because the linker will use whichever it finds. It is possible to tell the linker exactly which kind to use if both kinds exist but you don't need that complexity just now! The compiler also knows where system stuff is without you having to tell it: use 'gcc' for C code and 'g++' for C++ code.

Incidentally, "-I" does the same as "-L" but for header files (again with some quirks).

Code:

$ gcc main.c -L/home/pi/cpp and -lbcm2835
..compiles C file "main.c" looking for either of ("libbcm2835.so" | "libbcm2835.a") and will also look for it in "/home/pi/cpp" (-L /home/pi/cpp) whether it makes sense or not. The executable will be "a.out" (because we didn't tell the compiler what to call it).

Statistics: Posted by swampdog — Thu Mar 14, 2024 4:52 pm



Viewing all articles
Browse latest Browse all 5288

Trending Articles