undefined reference during AVR building, linker error - avr-gcc

I'm trying to compile and link AVR based program in Atmel Studio (7). I have got the error message in linking phase.
I am doing this procedure first time with this software, because I got this software for maintenance. And I heard that this software have been succesfully built previously in "some environment". I installed the newest Atmel Studio, and picked project file, and selected correct processor. After the linking phase started, the error message appeared regarding undefined reference.
Building target: 16k.elf Invoking: AVR/GNU Linker : 5.4.0
"C:\Program Files
(x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe"
-o 16k.elf DanskOFP.o adc_drv.o -Wl,-Map="16k.map" -Wl,--start-group -Wl,--end-group -Wl,--gc-sections -mmcu=atmega164p -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.2.209\gcc\dev\atmega164p"
C:\Projektit\V2.34\DanskOFP\Atmega164p\Makefile(118,1): error: recipe
for target '16k.elf' failed DanskOFP.o: In function `CPRX':
C:\Projektit\V2.34\DanskOFP\Atmega164p\Makefile(118,1): error: recipe
for target '16k.elf' failed DanskOFP.o: In function CPRX':
C:\Projektit\V2.34\DanskOFP\DanskOFP.c(1507,1): error: undefined
reference to__eewr_byte_m164p'
C:\Projektit\V2.34\DanskOFP\DanskOFP.c(1507,1): error: undefined
reference to __eewr_byte_m164p'
C:\Projektit\V2.34\DanskOFP\DanskOFP.c(1507,1): error: undefined
reference to__eewr_byte_m164p'
C:\Projektit\V2.34\DanskOFP\DanskOFP.c(1507,1): error: undefined
reference to __eewr_byte_m164p'
C:\Projektit\V2.34\DanskOFP\DanskOFP.c(1507,1): error: undefined
reference to__eewr_byte_m164p'
I expect that the internal definition regarding "__eewr_byte_m164p" should be found from somewhere, but I have no glue, has something changed between AVR versions, or should the definition presented some new practise ?

I guess that's a function from avr-libc, namely the target-specific function called from eeprom_write_byte(). Make sure that avr-libc in in the build/link path.

Related

Can't link against static library with Mingw on Linux

I have installed the GMP library and try to cross-compile with mingw-w64-posix.
My Library is in /usr/local/lib.
My compile command looks like the following:
x86_64-w64-mingw32-g++-posix src/factorial.cpp -o bin/factorial.win.o -I/usr/local/include -L/usr/local/lib -lgmp -lgmpxx
It throws an undefined reference error:
(I can remove the whole block from -L...., same error. Seems like the library doesnt link for some reason)
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccxY03WS.o:factorial.cpp:(.text$_ZN23__gmp_binary_multiplies4evalEP12__mpz_structPKS0_S3_[_ZN23__gmp_binary_multiplies4evalEP12__mpz_structPKS0_S3_]+0x27): undefined reference to `__gmpz_mul'
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccxY03WS.o:factorial.cpp:(.text$_ZN23__gmp_binary_multiplies4evalEP12__mpz_structPKS0_l[_ZN23__gmp_binary_multiplies4evalEP12__mpz_structPKS0_l]+0x26): undefined reference to `__gmpz_mul_si'
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccxY03WS.o:factorial.cpp:(.text$_ZN10__gmp_exprIA1_12__mpz_structS1_E7init_siEl[_ZN10__gmp_exprIA1_12__mpz_structS1_E7init_siEl]+0x1a): undefined reference to `__gmpz_init_set_si'
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccxY03WS.o:factorial.cpp:(.text$_ZN10__gmp_exprIA1_12__mpz_structS1_EC1EOS2_[_ZN10__gmp_exprIA1_12__mpz_structS1_EC1EOS2_]+0x2e): undefined reference to `__gmpz_init'
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccxY03WS.o:factorial.cpp:(.text$_ZN10__gmp_exprIA1_12__mpz_structS1_ED1Ev[_ZN10__gmp_exprIA1_12__mpz_structS1_ED1Ev]+0x14): undefined reference to `__gmpz_clear'
collect2: error: ld returned 1 exit status
However if i change my compiler to g++ instead everything works fine.
OK -
The link errors (__gmpz_init, __gmpz_clear, etc.) are GMP "internals". They're supposed to come from libgmp, the C-language base library.
The code that's referencing them (.text$ZN23__gmp_binary_multiplies4evalEP12__mpz_structPKS0_S3[ZN23__gmp_binary_multiplies4evalEP12__mpz_structPKS0_S3], etc.) is "name mangled" C++.
I suspect the problem is that your "gmpxx" library was built with a different C++ compiler, that has a different "name mangling" convention than MinGW.
SOLUTION:
Download the complete libGMP source (e.g. from https://gmplib.org/, and rebuild EVERYTHING (including libgmpxx) with your libmingw-w64-posix++ C++ cross-compiler.
ADDENDUM:
I downloaded gmp-6.2.1 source, and found __gmpz_clear here:
gmp-6.2.1\gmp-h.in
#define mpz_clear __gmpz_clear
__GMP_DECLSPEC void mpz_clear (mpz_ptr);
"gmp-h.in" is a template used by the project's "autoconf", to generate the libGMP source files for the specified target environment.
Which, in turn, means:
The project you started out with (in your original question) wasn't configured for MinGW
... and ...
You didn't run "configure" correctly when you tried building from source.
SUGGESTION:
Try building libGMP from source again. DELETE everything, re-extract from the libGMP tarball, and carefully follow the INSTALL instructions:
./configure
make
make check <= VERY IMPORTANT!!
make install
I'm curious about your build environment (Windows? Linux?), compiler (exact MinGW version) and target (if you're building on a Windows workstation, do you want to run your GMP app as a Windows .exe)?

Cepstral Example Compile Error

I'm trying to build a C++ program that incorporates Cepstral's voice synthesis features. The library was already downloaded on the machine by my predecessor but apparently he never got around to using it. The documentation is pretty much non-existent (there is a single forum that doesn't really have anything helpful). All of their included examples are in C and lack a MakeFile so I'm trying to compile them myself. The little documentation there is simply tells me I need to link to the libswift.so library.
It is a Ubuntu 12.04.5 OS and I tried this compile command
g++ -Wall -g tts.c -o tts -I/opt/swift/include -L/opt/swift/lib -lswift -lm
Which gives this error
/opt/swift/lib/libswift.so: undefined reference to 'sin'
/opt/swift/lib/libswift.so: undefined reference to 'exp'
/opt/swift/lib/libswift.so: undefined reference to 'cos'
/opt/swift/lib/libswift.so: undefined reference to 'log'
/opt/swift/lib/libswift.so: undefined reference to 'pow'
collect2: ld returned 1 exit status
This seems to indicate that -lm is not properly linking for some reason (I also tried switching the order of m and swift for the same result).
I usually use C++ and have very little C experience. I also frequently use Makefiles or edit previous/provided Makefiles and rarely call g++ itself so it's possible I'm missing something fairly obvious. Thanks.

g++ undefined reference though all files included

I have the problem that as the g++ tries to link the object files I receive the following error:
11:29:13 **** Build of configuration Debug for project daytime ****
make all
'Building target: daytime'
'Invoking: Cross G++ Linker'
g++ -o "daytime" ./tcf/services/daytime.o ./tcf/main/main.o
./tcf/services/daytime.o: In function `command_get_time_of_day':
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:38: undefined reference to `json_read_string'
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:40: undefined reference to `exception'
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:43: undefined reference to `exception'
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:52: undefined reference to `write_stringz'
makefile:46: recipe for target 'daytime' failed
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:54: undefined reference to `write_stringz'
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:56: undefined reference to `write_errno'
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:58: undefined reference to `json_write_string'
./tcf/services/daytime.o: In function `ini_daytime_service':
C:\Users\falkstef\runtime-EclipseApplication\daytime\Debug/../tcf/services/daytime.c:70: undefined reference to `add_command_handler'
collect2.exe: error: ld returned 1 exit status
make: *** [daytime] Error 1
I have no idea why this is the case since e.g. #include <tcf/framework/json.h>is included and found.
Didn't gcc compile the corresponding *.c files such that this linker error occurs?
What is the problem here?
Thank you.
It is not enough to include the header files; you also have to specify the libraries where those functions are defined.
To make the linker find all those methods/classes (json_read_string, write_stringz, exception) you need to reference the library. If e.g. they are contained in a library called libjson.so, you should do:
g++ -ljson -o "daytime" ./tcf/services/daytime.o ./tcf/main/main.o
(or add the library to the project options, if eclipse is managing your make files).
Or if it's another .o file, include that in the compilation (-> or in the project, if eclipse is creating the make file).

Linker error: undefined reference to symbol 'pthread_rwlock_trywrlock##GLIBC_2.2.5'

I've been developing with CentOS, Qt 4.7, and GCC 4.4
I've just installed Red Hat Developer Toolset 1.1 which includes GCC 4.7.2, and at the end of make, I get an error
/usr/bin/ld: ../../bin/Solo: undefined reference to symbol 'pthread_rwlock_trywrlock##GLIBC_2.2.5'
/usr/bin/ld: note: 'pthread_rwlock_trywrlock##GLIBC_2.2.5' is defined in DSO /lib64/libpthread.so.0 so try adding it to the linker command line
/lib64/libpthread.so.0: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
I'm guessing that Qt threads is referencing to that. How can I fix it?
You want to compile with -pthread which does more than just link with libpthread:
Adds support for multithreading with the pthreads library. This
option sets flags for both the preprocessor and linker.
You just need to add CONFIG += thread to your .pro file.
Read the note: try to add /lib64/libpthread.so.0 into Makefile (-lpthread after gcc command, or /lib64/libpthread.so.0 after ld (or after gcc -shared)), or something like LIB += -lpthread if there's such definition somewhere.
See also: Adding external library into Qt Creator project and http://www.qtcentre.org/threads/39144-How-to-add-a-lib-to-a-qt-project
Btw, post your Makefile, so somebody will be able to point to exact line.
In my little laptop Linux (where I have a mixed bag of libraries),
I just had to add
LDFLAGS=-lpthread
AT THE END of the configure command arguments.
After that, make did its job perfectly (With the existing libraries).
I received similar 'Linker error: undefined reference to symbol' errors attempting to compile and install Python-3.7.2 on FreeBSD 12.
/usr/bin/ld: error: undefined symbol: _Py_GetGlobalVariablesAsDict
/usr/bin/ld: error: undefined symbol: _PyCoreConfig_AsDict
/usr/bin/ld: error: undefined symbol: _PyMainInterpreterConfig_AsDict
Resolved by passing LDFLAGS=-lpthread directly to make of lang/python37 or by adding to /etc/make.conf.
If using portmaster to install/update use -m to pass the arguement to make e.g.portmaster -a -m 'LDFLAGS=-lpthread'

undefined reference to MinFilter and MaxFilter in StereoMatch code

everyone
Recently, I'm working on stereo vision. And I download the StereoMatch code from middlebury website: http://vision.middlebury.edu/stereo/code/
I used Ubuntu 11.10 to run this code. After I run this code, I got the following error. There are 3 'undefined reference to ' error. But the code has already included the header file 'MinFilter.h'. And I get confused. Could someone help me? I would appreciate it.
errors:
StcAggregate.o: In function CStereoMatcher::AggrMin()':
StcAggregate.cpp:(.text+0x20f): undefined reference tovoid MinFilter(CImageOf&, CImageOf&, int, int)'
StcEvaluate.o: In function _ZL14PartialShuffle8CImageOfIhERS0_S1_f.constprop.2':
StcEvaluate.cpp:(.text+0x37): undefined reference tovoid MinFilter(CImageOf&, CImageOf&, int, int)'
StcEvaluate.cpp:(.text+0x5b): undefined reference to `void MaxFilter(CImageOf&, CImageOf&, int, int)'
collect2: ld returned 1 exit status
make: * [StereoMatch] Error 1
This is an error from the linker (and/or runtime environment) which cannot find some symbols (i.e. code) which the header files you've included promised to exist. In order for these symbols to be found, you must tell the linker to link against the library (or object file) containing them. This library may have come with the code pre-compiled or may have been made during installation. Have you not got a README file where it is explained how to use (i.e. link and run) the code?
I met exactly the same problem and solved it like this:
Open the Makefile and change the first line from
CPPFLAGS = -O2 -Wall
to
CPPFLAGS = -O -Wall
Then "make clean" and "make", it works for me. So I guess it is because param
"-O2" does more optimization than expected.