Eclipse GCC C Cross Compiling for OpenWrt not working - c

I am a beginner in both linux and openwrt. Sorry if this is a stupid question.
I am following this tutorial : https://downloads.openwrt.org/docs/eclipse.pdf
Chip : MT7620
Toolchain Prefix : mipsel-openwrt-linux-
Toolchain Path : /ligo/openwrt/staging_dir/toolchain-mipsel_24kec+dsp_gcc-5.3.0_musl-1.1.14
Code:
#include <stdio.h>
void main()
{
printf("Hello World");
}
Error:
**** Build of configuration Debug for project hello ****
make all
Building target: hello
Invoking: Cross GCC Linker
mipsel-openwrt-linux-gcc -L/ligo/openwrt/staging_dir/toolchain-mipsel_24kec+dsp_gcc-5.3.0_musl-1.1.14/lib -o"hello" ./main.o
mipsel-openwrt-linux-gcc: warning: environment variable 'STAGING_DIR' not defined
mipsel-openwrt-linux-gcc: warning: environment variable 'STAGING_DIR' not defined
/ligo/openwrt/staging_dir/toolchain-mipsel_24kec+dsp_gcc-5.3.0_musl-1.1.14/lib/gcc/mipsel-openwrt-linux-musl/5.3.0/../../../../mipsel-openwrt-linux-musl/lib/crt1.o: In function `_start_c':
/ligo/openwrt/build_dir/toolchain-mipsel_24kec+dsp_gcc-5.3.0_musl-1.1.14/musl-1.1.14/crt/crt1.c:17: undefined reference to `main'
/ligo/openwrt/build_dir/toolchain-mipsel_24kec+dsp_gcc-5.3.0_musl-1.1.14/musl-1.1.14/crt/crt1.c:17: undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [hello] Error 1

add the following 2 lines at the end of bashrc
export PATH=$PATH:~/openwrt/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin
export STAGING_DIR=~/openwrt/staging_dir
source ~/.bashrc
To open/create the bashrc you can execute
sudo gedit ~/.bashrc

Well I feel stupid when I have to figure this problem which happens too often.
I get the above error when I forget to save the file that contains main() it gives this error since the file is empty until you save it.
These instructions tell how to automatically save all the modified files when you kick off a build Save before build Keep this set and you won't see this again.
If you don't have the path to the toolchain set in eclipse, it never even finds a compiler, assembler or linker so it can not produce the error given. The warnings about STAGING_DIR are harmless in this case.

Related

Compiling C error - CS50 course - week 1 - Linux

i'm trying to compile a simple code by following the steps that the professor made but I have this issue
cc calculator.c -o calculator
/usr/bin/ld: /tmp/ccznJMZB.o: in function `main':
calculator.c:(.text+0x19): undefined reference to `get_int'
/usr/bin/ld: calculator.c:(.text+0x2d): undefined reference to `get_int'
collect2: error: ld returned 1 exit status
make: *** [<builtin>: calculator] Error 1
I'm using Linux mint btw, already had problems installing cs50 library but that's done. I'm happy to get a new error tho, but I need to fix it.
Thanks!
See https://cs50.readthedocs.io/libraries/cs50/c/
To use these functions, make sure to include #include <cs50.h> atop your file and compile with the -lcs50 flag.
#include <cs50.h> requires the header file cs50.h to be in the include path of your compiler.
#include "cs50.h" expects the header file cs50.h to be in the same directory as your .cfile.
The link above also tells you how to install the library:
$ curl -s https://packagecloud.io/install/repositories/cs50/repo/script.deb.sh | sudo bash
$ sudo apt install libcs50
Troubleshooting
If when compiling your program, you see:
/usr/bin/ld: cannot find -lcs50:
Add export LIBRARY_PATH=/usr/local/lib to your .bashrc.
fatal error: 'cs50.h' file not found:
Add export C_INCLUDE_PATH=/usr/local/include to your .bashrc.
cannot open shared object file: No such file or directory:
Add export LD_LIBRARY_PATH=/usr/local/lib to your .bashrc.

Installing AODV protocol on Raspberry pi (https://github.com/erimatnor/aodv-uu)

I'm trying to install AODV protocol on Raspberry pi. After completing git clone from "https://github.com/erimatnor/aodv-uu" when I tried to do "make" and I am getting below error. Expecting your suggestion. Thank you!
make
gcc -Wall -O3 -g -DDEBUG -DCONFIG_GATEWAY -DDEBUG -o aodvd main.o list.o debug.o timer_queue.o aodv_socket.o aodv_hello.o aodv_neighbor.o aodv_timeout.o routing_table.o seek_list.o aodv_rreq.o aodv_rrep.o aodv_rerr.o nl.o locality.o
aodv_neighbor.o: In function neighbor_add':
/home/pi/aodv-uu/aodv_neighbor.c:68: undefined reference tohello_update_timeout'
aodv_timeout.o: In function route_discovery_timeout':
/home/pi/aodv-uu/aodv_timeout.c:98: undefined reference tort_table_update_timeout'
aodv_rreq.o: In function rreq_route_discovery':
/home/pi/aodv-uu/aodv_rreq.c:460: undefined reference tort_table_update_timeout'
aodv_rreq.o: In function rreq_local_repair':
/home/pi/aodv-uu/aodv_rreq.c:521: undefined reference tort_table_update_timeout'
aodv_rrep.o: In function rrep_forward':
/home/pi/aodv-uu/aodv_rrep.c:231: undefined reference tort_table_update_timeout'
nl.o: In function nl_kaodv_callback':
/home/pi/aodv-uu/nl.c:282: undefined reference tort_table_update_timeout'
collect2: error: ld returned 1 exit status
Makefile:112: recipe for target 'aodvd' failed
make: *** [aodvd] Error 1
The code available from sourceforge is the same code that is on github. If you download the archive, you'll see that the latest modification date of any file there is 2010. Given the age of this code, I wouldn't be surprised to find that things simply don't work anymore.
However, here's a quick workaround for your problem. The root cause appears to be that the problem functions, like rt_table_update_timeout, are declared as inline, but that information seems to get lost somewhere in the build process such that other object files are trying to reference these as non-inline functions.
You can avoid this by opening defs.h, looking for this line:
#define NS_INLINE inline
And replace it with:
#define NS_INLINE
This will allow aodvd to compile correctly (make aodvd). On my system, the kernel module will subsequently fail to compile:
cc1: fatal error: /lib/modules/4.13.15-100.fc25.x86_64/build/include/linux/modversions.h: No such file or directory
As far as I can tell, the modversions.h file is no longer produced by modern Linux kernels.

Undefined reference to main error very persistent

I'm using the Eclipse Oxygen IDE on Linux, and I am trying to code in C. There are some problems though.
Whatever code I try to build, even perfectly valid code, returns this error:
make all
Building target: heyson
Invoking: GCC C Linker
gcc -o "heyson" ./heyson.o
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
makefile:29: recipe for target 'heyson' failed
collect2: error: ld returned 1 exit status
make: *** [heyson] Error 1
This is all of my code:
#include <stdio.h>
int main(){
printf("hello");
return 0;
}
It's literally a hello-world program. I tried putting main(void), but that returned the same error, I tried removing the #include line but that kept it the same, I did nigh everything possible to this and it keeps spitting out the same thing.
All I did, was install the C/C++ version of eclipse, use the Linux GCC toolchain to create a project, create a source file called heyson.c, and put this code in, trying to build it. After some time I realised that everything I put in returned the same thing, so I'm at the point of desperation...
UPDATE: I decided to uninstall Eclipse and use CLion instead. It works brilliantly, so I can only conclude that the problem was the IDE itself.

How do I get pthreads to work in Windows?

I was running into errors such as those mentioned bellow when trying to compile code containing pthreads
warning: return type defaults to 'int' [-Wreturn-type]|
|In function 'print_message_function':|
warning: control reaches end of non-void function [-Wreturn-type]|
| undefined reference to `_imp__pthread_create'|
| undefined reference to `_imp__pthread_create'|
| undefined reference to `_imp__pthread_join'|
| undefined reference to `_imp__pthread_join'|
I'm running GCC on Windows 7 but I have mingw installed. I'm using the IDE Code::Blocks and select "compile current file". Here is a screen shot of the linker settings, I'm at a loss here
UPDATE: I added -pthread to the "Other linker options" and it works better. There still are problems. When I compile it says
|In function 'print_message_function':|
warning: control reaches end of non-void function [-Wreturn-type]|
and when I go to run it CodeBlocks says "it appears the program has not been built yet" and when I click on "build" I am shown this error
mingw32-g++.exe -o "SimpleExample.exe" "SimpleExample.o" -static-libgcc -static-libstdc++ -pthread
mingw32-g++.exe: error: unrecognized option '-pthread'
Process terminated with status 1 (0 minutes, 0 seconds)
0 errors, 1 warnings (0 minutes, 0 seconds)
How do I fix this? I want to build/test on Windows but have the program run on a Unix environment. What is the difference between compile and build in an IDE?
this answer come late but ... it worked for me, so I decided to share it.
I downloaded the pthreads library from
ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-9-1-release.zip
I unzipped it, then I copyed header files (.h) in
C:\ProgramFiles\CodeBlocks\MinGW\include
and library files in
C:\ProgramFiles\CodeBlocks\MinGW\lib
I copyed the dll files in the executable folder of my project
(myprojects/bin/debug in my case)
I added the -lpthreadGC2 option in the
Settings -> Compiler -> Linker Settings -> Other Linker Options
of my Code::Blocks IDE
Hope this can help.
It is -lpthread, not -pthread.
Edit:
Libraries can be added to the compile line in a couple of ways. If we have a file called (for example) /usr/lib/libpthread.so we could include the file like this:
cc -o myprog /usr/lib/libpthread.so myprog.c
or, alternatively:
cc -o myprog -lpthread -L /usr/lib myprog.c
Since /usr/lib is a standard directory, we don't normally require the -L option. At runtime we might have to set an environment variable:
export LD_LIBRARY_PATH=/usr/lib
but again, the standard libraries are defaulted, so you don't have to use this unless you are building your own or using 3rd-party libraries.
warning: control reaches end of non-void function [-Wreturn-type]|
Your main does not return a value. Add return 0; at the end of main.
| undefined reference to `_imp__pthread_create'|
You need to link with the thread library. Add -lpthread to the linker command line.
Here's what happens currently as of now when using MinGW Installation Manager (the mingw32 package manager for windows) under Windows with the following packages installed:
mingw32-libpthreadgc-dll
mingw32-libpthreadgce-dll
ERROR: gcc 5.3.0 fails linking pthread e.g.
c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot find -lpthread
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second
SOLUTION: include sources from MinGW Package Manager, too, i.e. also select
mingw32-libpthreadgc-dev
mingw32-libpthreadgce-dev
MinGW 4.9.2 does not show this effect. GCC 5.4 on Ubuntu also does not require the pthread sources to compile any code.
This one helped me out whilst other tries (using mingw32-libpthread-old or configuring linker settings) failed.

Linker command failed with exit code 1 In Xcode

I'm writing C program in Xcode, and I got this problem:
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any idea to solve it?
Note: I've just update my OS to Mountain Lion yesterday.
A common error is to define a symbol in a header when you wanted to declare it:
When you for instance declare a global variable and forget the extern or you define an inline function and forget the inline. In these cases the compiler emits the symbol in each compile unit that includes this header and you end up with multiple definitions of a symbol.
Anyway you should just look for the symbol in question.

Resources