marcellatwin
Posts: 32
Joined: Sat Jan 27, 2018 6:41 pm
Location: USA

Trouble with GCC compiling

Thu Jun 06, 2019 6:33 pm

Hello everyone.

I am working on a CAN bus project using a RPi 3B with Raspian Stretch (9.9). It has a CAN shield, PiCan2, to connect to the CAN bus thru an OBD2 port on a car. I have a custom program built from code provided by the linux-can-utils which has programs such as candump, cansniffer, cansend, etc. I've gotten my program to compile, run, and read CAN messages just fine. However, I'm starting to move forward with my project where I need to send messages as well. So I included the code for sending messages from the cansend source code and included the additional libraries that cansend had

Code: Select all

#include "lib.h"
Also during compiling I have tried to include the location of the lib.h

Code: Select all

gcc -Wall -Wextra -O3 /home/pi/jc_volvo.c -lwiringPi -lcurses -lpthread -I/home/pi/linux-can-util -llib -o /home/pi/jc_volvo
And this is what I get:

Code: Select all

/usr/bin/ld: cannot find -llib
collect2: error: ld returned 1 exit status
I feel like the code is fine but something with the library or compiling is not working. Any ideas? Thanks in advance anyone's help.

User avatar
PeterO
Posts: 4722
Joined: Sun Jul 22, 2012 4:14 pm

Re: Trouble with GCC compiling

Thu Jun 06, 2019 6:43 pm

Should that be -Llib (note the first L is a capital)
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

marcellatwin
Posts: 32
Joined: Sat Jan 27, 2018 6:41 pm
Location: USA

Re: Trouble with GCC compiling

Thu Jun 06, 2019 7:11 pm

PeterO wrote:
Thu Jun 06, 2019 6:43 pm
Should that be -Llib (note the first L is a capital)
PeterO
So I tried that, even though I didn't think it would work cause all the other libraries included in the compilation are -l "lowercase". Why would this one be different? So I did that and got the following error:

Code: Select all

/tmp/ccqdvsBH.o: In function 'can_send_thread':
undefined reference to 'parse_canframe'
So something changed. But 'parse_canframe' is one of the functions that is included by the lib.h. So I'm not sure what's going on.

User avatar
PeterO
Posts: 4722
Joined: Sun Jul 22, 2012 4:14 pm

Re: Trouble with GCC compiling

Thu Jun 06, 2019 7:24 pm

Where did you get the compile options from ?
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

marcellatwin
Posts: 32
Joined: Sat Jan 27, 2018 6:41 pm
Location: USA

Re: Trouble with GCC compiling

Thu Jun 06, 2019 8:08 pm

PeterO wrote:
Thu Jun 06, 2019 7:24 pm
Where did you get the compile options from ?
PeterO

Code: Select all

gcc -Wall -Wextra -O3 /home/pi/jc_volvo.c -lwiringPi -lcurses -lpthread -I/home/pi/linux-can-util -llib -o /home/pi/jc_volvo
-Wall - is all the compiler warnings turned on, these aren't errors but warnings
-Wextra - extra compiler warnings that are not included in Wall
-O3 - is all optimizations turned on, like inlining, it takes longer to compile but only by a few seconds for me.

Is this what you mean?

User avatar
PeterO
Posts: 4722
Joined: Sun Jul 22, 2012 4:14 pm

Re: Trouble with GCC compiling

Thu Jun 06, 2019 8:10 pm

where has the -llib come from ?

I doubt there is a library called "liblib.so"

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
PeterO
Posts: 4722
Joined: Sun Jul 22, 2012 4:14 pm

Re: Trouble with GCC compiling

Thu Jun 06, 2019 10:04 pm

Now I'm on a machine which has the can-utils installed on it so I can see how it is supposed to be working.

Code: Select all

~/CAN/linux-can-utils$ ls -l lib*
-rw-r--r-- 1 petero petero 11770 Mar 15  2015 lib.c
-rw-r--r-- 1 petero petero  5800 Mar 15  2015 lib.h
-rw-r--r-- 1 petero petero 10400 Mar 15  2015 lib.o
So there is no library called "lib", just lib.c which is compiled to lib.o

The original make files has all the targets depending on lib.o so that will get included in the link command. as just "lib.o"

Since it isn't a propper library (.so or .a) I would suggest just moving lib.c and lib.h into the directory with your other source code and use it like the original can uttils makefile does.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

marcellatwin
Posts: 32
Joined: Sat Jan 27, 2018 6:41 pm
Location: USA

Re: Trouble with GCC compiling

Fri Jun 07, 2019 3:54 pm

PeterO wrote:
Thu Jun 06, 2019 10:04 pm
Now I'm on a machine which has the can-utils installed on it so I can see how it is supposed to be working.

Code: Select all

~/CAN/linux-can-utils$ ls -l lib*
-rw-r--r-- 1 petero petero 11770 Mar 15  2015 lib.c
-rw-r--r-- 1 petero petero  5800 Mar 15  2015 lib.h
-rw-r--r-- 1 petero petero 10400 Mar 15  2015 lib.o
So there is no library called "lib", just lib.c which is compiled to lib.o

The original make files has all the targets depending on lib.o so that will get included in the link command. as just "lib.o"

Since it isn't a propper library (.so or .a) I would suggest just moving lib.c and lib.h into the directory with your other source code and use it like the original can uttils makefile does.

PeterO
First off, thank you for looking into this and being patient with me. I really don't understand libraries that well.

So should I do what I've done with the other libraries I use when I just have the ".h" file in the same directory as the .c file I'm compiling?
Should I move the .c AND .h?
Do I compile with -llib like the other libraries or -lib?

bzt
Posts: 373
Joined: Sat Oct 14, 2017 9:57 pm

Re: Trouble with GCC compiling

Sun Jun 09, 2019 5:57 pm

Hi,
marcellatwin wrote:
Fri Jun 07, 2019 3:54 pm
First off, thank you for looking into this and being patient with me. I really don't understand libraries that well.
That's simple. There're two kinds of libraries: statically linked and dynamically linked.

Statically linked libraries have the extension .a (stands for object archive), and you link with them at compile time. The library will be included in your executable, growing it's size.

Dynamically linked libraries on the other hand have the extension .so (shared object), and are typically located in /lib or /usr/lib. At compilation time, the linker just creates a reference to them. You can list those references with the "ldd" command, for example try "ldd /bin/bash". Those libraries are linked when the user executes the binary, therefore they must exists on the target machine (exactly like DLLs on Windows. As a matter of fact, DLL stands for Dynamically Linked Library).

Now -L (uppercase) specifies the directory in which the linker should look for libraries, so for example -L/usr/lib. This is usually already set up for you correctly, no need to use.
The -l (lowercase) specifies the name of the library to load, prefixed and suffixed by (-L agrument)"lib"(the -l argument)(".so" or ".a"). For example in case of -lm your executable will be linked with /lib/libm.so. That all.

marcellatwin wrote:
Fri Jun 07, 2019 3:54 pm
So should I do what I've done with the other libraries I use when I just have the ".h" file in the same directory as the .c file I'm compiling?
Should I move the .c AND .h?
Do I compile with -llib like the other libraries or -lib?
As @PeterO suggested, don't think of it as a library. Just copy the .c and .h into your source directory and compile with the rest of your code. Then when you link, list lib.o with the rest of your object files. For example:

Code: Select all

gcc -Wall -Wextra -O3 -c /home/pi/jc_volvo.c -o jc_volvo.o
gcc -Wall -Wextra -O3 -c /home/pi/lib.c -o lib.o
gcc jc_volvo.o lib.o -o jc_volvo -lwiringPi -lcurses -lpthread
Some caveats: always use "-c" on individual .c files if you have more, then another gcc command to link the ".o"s together. This will save you lots of trouble later. Second, you must specify the libraries on the command line AFTER the -o argument, some gcc versions are very strict in this regard.

To automate the compilation and linking, here's a very simple GNU make Makefile:

Code: Select all

TARGET = jc_volvo
SRCS = $(wildcard *.c)
OBJS = $(SRCS:.c=.o)

all: $(TARGET)

%.o: %.c
        gcc -Wall -Wextra -O3 -c $< -o DOLLARAT

$(TARGET): $(OBJS)
        gcc $(OBJS) -o $(TARGET) -lwiringPi -lcurses -lpthread
You should replace DOLLARAT with a $ and an @ sign, but this forum replaces that with an "[email protected]" link so I couldn't wrote those directly in the code block. Also there's a TAB character in front of gcc and not 8 spaces (important).

Now to compile your project, you just type "make all" and hit Enter. If you create a new .c file, no need to modify anything, it will be automagically compiled into your binary :-) This will considerably speed up your compilation time, because it will only compile the .o files for the .c sources you have modified, and not all of them.

Cheers,
bzt

User avatar
PeterO
Posts: 4722
Joined: Sun Jul 22, 2012 4:14 pm

Re: Trouble with GCC compiling

Sun Jun 09, 2019 6:26 pm

Sorry, I missed the last post from marcellatwin.
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Return to “C/C++”