Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid of multiple definition errors #41

Closed
wants to merge 2 commits into from

Conversation

uncleura
Copy link

We do #include tft_lib.h throughout the program, and the tft.h file has its own protection against multiple inclusion: #ifndef __TFT_LIB_H__ . However, this protection fails when we first include the tft_lib.h file when building the driver.a library and then try to build the main program that also #includes tft_lib.h along with the library driver.a .
Global vars (*DrawPixel ... *InversionOn) which we declare in this tft_lib.h file should be declared only once as "global vars" . And additionally we can include them as =external= variables in as many files as we want.
The *.c file name, consonant with the name of the tft_lib.h, I mean tft_lib.c, suggests that for any set of modules in the final program, where the tft_lib.h file is included, this set will always have the tft_lib.c module. So, we do declare global vars when we include tht_lib.h to tft_lib.c and do extern vars declarations when we include tft_lib.h to other *.c files.

uncleura added 2 commits May 11, 2022 14:39
__TFT_LIB_C__ definition must be before inclusion of "tft_lib.h"
Pointers to driver dependent functions should be declared es "extern" everywhere we #include this header file, but once - as pointers linker should reserve in memory - if this #include was from tft_lib.c file.
@nopnop2002
Copy link
Owner

nopnop2002 commented May 11, 2022

No error in my environment.

You are doing something wrong.

pi@raspberrypi:~ $ cd Raspberry-ili9325/
pi@raspberrypi:~/Raspberry-ili9325 $ make ILI9341
cc    -c -o driver/st7781.o driver/st7781.c
cc    -c -o driver/ili9342.o driver/ili9342.c
cc    -c -o driver/ili9488.o driver/ili9488.c
cc    -c -o driver/ili9327.o driver/ili9327.c
cc    -c -o driver/ili9486.o driver/ili9486.c
cc    -c -o driver/ili9481.o driver/ili9481.c
cc    -c -o driver/s6d1121.o driver/s6d1121.c
cc    -c -o driver/ili9225.o driver/ili9225.c
cc    -c -o driver/ili9341.o driver/ili9341.c
cc    -c -o driver/ili9325.o driver/ili9325.c
cc    -c -o driver/lgdp4532.o driver/lgdp4532.c
cc    -c -o driver/ili9320.o driver/ili9320.c
cc    -c -o driver/r61509.o driver/r61509.c
ar crv driver.a driver/st7781.o driver/ili9342.o driver/ili9488.o driver/ili9327.o driver/ili9486.o driver/ili9481.o driver/s6d1121.o driver/ili9225.o driver/ili9341.o driver/ili9325.o driver/lgdp4532.o driver/ili9320.o driver/r61509.o
a - driver/st7781.o
a - driver/ili9342.o
a - driver/ili9488.o
a - driver/ili9327.o
a - driver/ili9486.o
a - driver/ili9481.o
a - driver/s6d1121.o
a - driver/ili9225.o
a - driver/ili9341.o
a - driver/ili9325.o
a - driver/lgdp4532.o
a - driver/ili9320.o
a - driver/r61509.o
driver.a make success
cc    -c -o tjpgd2/decode_jpeg.o tjpgd2/decode_jpeg.c
cc    -c -o tjpgd2/tjpgd.o tjpgd2/tjpgd.c
ar crv jpeg.a tjpgd2/decode_jpeg.o tjpgd2/tjpgd.o
a - tjpgd2/decode_jpeg.o
a - tjpgd2/tjpgd.o
jpeg.a make success
cc    -c -o pngle/pngle.o pngle/pngle.c
cc    -c -o pngle/decode_png.o pngle/decode_png.c
cc    -c -o pngle/miniz.o pngle/miniz.c
ar crv png.a pngle/pngle.o pngle/decode_png.o pngle/miniz.o
a - pngle/pngle.o
a - pngle/decode_png.o
a - pngle/miniz.o
png.a make success
cc -o demo demo.c fontx.c tft_lib.c driver.a jpeg.a png.a -lwiringPi -lm -lpthread -DILI9341
cc -o draw draw.c fontx.c tft_lib.c driver.a jpeg.a png.a -lwiringPi -lm -lpthread -DILI9341
cc -o rgb2color rgb2color.c
cc -o jpegtest jpegtest.c fontx.c tft_lib.c driver.a jpeg.a png.a -lwiringPi -lm -lpthread -DILI9341
cc -o pngtest pngtest.c fontx.c tft_lib.c driver.a jpeg.a png.a -lwiringPi -lm -lpthread -DILI9341
pi@raspberrypi:~/Raspberry-ili9325 $

@uncleura
Copy link
Author

No, i don't think so. Look, up to "png.a make success" message I got the same, but when demo.c start to compiling, I receive errors:
$ make ILI9341
cc -c -o driver/ili9225.o driver/ili9225.c
cc -c -o driver/ili9320.o driver/ili9320.c
cc -c -o driver/ili9325.o driver/ili9325.c
cc -c -o driver/ili9327.o driver/ili9327.c
cc -c -o driver/ili9341.o driver/ili9341.c
cc -c -o driver/ili9342.o driver/ili9342.c
cc -c -o driver/ili9481.o driver/ili9481.c
cc -c -o driver/ili9486.o driver/ili9486.c
cc -c -o driver/ili9488.o driver/ili9488.c
cc -c -o driver/lgdp4532.o driver/lgdp4532.c
cc -c -o driver/r61509.o driver/r61509.c
cc -c -o driver/s6d1121.o driver/s6d1121.c
cc -c -o driver/st7781.o driver/st7781.c
ar crv driver.a driver/ili9225.o driver/ili9320.o driver/ili9325.o driver/ili9327.o driver/ili9341.o driver/ili9342.o driver/ili9481.o driver/ili9486.o driver/ili9488.o driver/lgdp4532.o driver/r61509.o driver/s6d1121.o driver/st7781.o
r - driver/ili9225.o
r - driver/ili9320.o
r - driver/ili9325.o
r - driver/ili9327.o
r - driver/ili9341.o
r - driver/ili9342.o
r - driver/ili9481.o
r - driver/ili9486.o
r - driver/ili9488.o
r - driver/lgdp4532.o
r - driver/r61509.o
r - driver/s6d1121.o
r - driver/st7781.o
driver.a make success
cc -c -o tjpgd2/decode_jpeg.o tjpgd2/decode_jpeg.c
cc -c -o tjpgd2/tjpgd.o tjpgd2/tjpgd.c
ar crv jpeg.a tjpgd2/decode_jpeg.o tjpgd2/tjpgd.o
r - tjpgd2/decode_jpeg.o
r - tjpgd2/tjpgd.o
jpeg.a make success
cc -c -o pngle/decode_png.o pngle/decode_png.c
cc -c -o pngle/miniz.o pngle/miniz.c
cc -c -o pngle/pngle.o pngle/pngle.c
ar crv png.a pngle/decode_png.o pngle/miniz.o pngle/pngle.o
r - pngle/decode_png.o
r - pngle/miniz.o
r - pngle/pngle.o
png.a make success
cc -o demo demo.c fontx.c tft_lib.c driver.a jpeg.a png.a -lwiringPi -lm -lpthread -DILI9341
/usr/bin/ld: /tmp/ccKMi9XL.o:(.bss+0x0): multiple definition of DrawPixel'; /tmp/ccv5daKO.o:(.bss+0x0): first defined here /usr/bin/ld: /tmp/ccKMi9XL.o:(.bss+0x4): multiple definition of DrawMultiPixels'; /tmp/ccv5daKO.o:(.bss+0x4): first defined here
/usr/bin/ld: /tmp/ccKMi9XL.o:(.bss+0x8): multiple definition of DrawFillRect'; /tmp/ccv5daKO.o:(.bss+0x8): first defined here /usr/bin/ld: /tmp/ccKMi9XL.o:(.bss+0xc): multiple definition of DisplayOff'; /tmp/ccv5daKO.o:(.bss+0xc): first defined here
/usr/bin/ld: /tmp/ccKMi9XL.o:(.bss+0x10): multiple definition of DisplayOn'; /tmp/ccv5daKO.o:(.bss+0x10): first defined here /usr/bin/ld: /tmp/ccKMi9XL.o:(.bss+0x14): multiple definition of InversionOff'; /tmp/ccv5daKO.o:(.bss+0x14): first defined here
/usr/bin/ld: /tmp/ccKMi9XL.o:(.bss+0x18): multiple definition of InversionOn'; /tmp/ccv5daKO.o:(.bss+0x18): first defined here /usr/bin/ld: driver.a(ili9341.o):(.bss+0x0): multiple definition of DrawPixel'; /tmp/ccv5daKO.o:(.bss+0x0): first defined here
/usr/bin/ld: driver.a(ili9341.o):(.bss+0x4): multiple definition of DrawMultiPixels'; /tmp/ccv5daKO.o:(.bss+0x4): first defined here /usr/bin/ld: driver.a(ili9341.o):(.bss+0x8): multiple definition of DrawFillRect'; /tmp/ccv5daKO.o:(.bss+0x8): first defined here
/usr/bin/ld: driver.a(ili9341.o):(.bss+0xc): multiple definition of DisplayOff'; /tmp/ccv5daKO.o:(.bss+0xc): first defined here /usr/bin/ld: driver.a(ili9341.o):(.bss+0x10): multiple definition of DisplayOn'; /tmp/ccv5daKO.o:(.bss+0x10): first defined here
/usr/bin/ld: driver.a(ili9341.o):(.bss+0x14): multiple definition of InversionOff'; /tmp/ccv5daKO.o:(.bss+0x14): first defined here /usr/bin/ld: driver.a(ili9341.o):(.bss+0x18): multiple definition of InversionOn'; /tmp/ccv5daKO.o:(.bss+0x18): first defined here
collect2: error: ld returned 1 exit status
make: *** [Makefile:58: ILI9341] Error 1

@uncleura uncleura closed this May 11, 2022
@uncleura uncleura reopened this May 11, 2022
@nopnop2002
Copy link
Owner

nopnop2002 commented May 11, 2022

This is my environment.
Something is different.

cc -o demo demo.c fontx.c tft_lib.c driver.a jpeg.a png.a -lwiringPi -lm -lpthread -DILI9341
cc -o draw draw.c fontx.c tft_lib.c driver.a jpeg.a png.a -lwiringPi -lm -lpthread -DILI9341
cc -o rgb2color rgb2color.c
cc -o jpegtest jpegtest.c fontx.c tft_lib.c driver.a jpeg.a png.a -lwiringPi -lm -lpthread -DILI9341
cc -o pngtest pngtest.c fontx.c tft_lib.c driver.a jpeg.a png.a -lwiringPi -lm -lpthread -DILI9341

pi@raspberrypi:~/Raspberry-ili9325 $ uname -a
Linux raspberrypi 5.10.103-v7+ #1529 SMP Tue Mar 8 12:21:37 GMT 2022 armv7l GNU/Linux

pi@raspberrypi:~/Raspberry-ili9325 $ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

@nopnop2002
Copy link
Owner

nopnop2002 commented May 12, 2022

The version of GCC has changed.
raspberrypi 5.10.103-v7+:gcc (Raspbian 8.3.0-6+rpi1) 8.3.0
raspberrypi 5.15.32-v7+:gcc (Raspbian 10.2.1-6+rpi1) 10.2.1 20210110

"Multiple definition of" on GCC 10.2.1 but not GCC 8.3.0

There was a change in behaviour about global variables between GCC 8.3.0 and GCC 10.2.1.
In C you are supposed to only define a global variable in one translation unit,
other translation unit that want to access the variable should declare it as "extern".

GCC 8.3.0 has a collaborative definition, so no error occurs.
But GCC 10.2.1 don't have cooperative definition, so error occurs.


Thank you for reporting and your code.

Your code has helped me a lot.

Try latest.

@uncleura
Copy link
Author

Glad to make my modest contribution. Thank you.

@uncleura uncleura closed this May 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants