Undefined reference to main error very persistent - c

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.

Related

MingGw error while trying to complie C code

I tried to compile my C code (using mingw) into a EXE file but it just says the following error:
/mingw/lib/libmingw32.a(setargv.o):(.text+0x33): undefined reference to `__chkstk_ms' /mingw/lib/libmingwex.a(glob.o):(.text+0x6d2): undefined reference to `__chkstk_ms' /mingw/lib/libmingwex.a(glob.o):(.text+0x8b8): undefined reference to `__chkstk_ms' /mingw/lib/libmingwex.a(glob.o):(.text+0x900): undefined reference to `__chkstk_ms' /mingw/lib/libmingwex.a(glob.o):(.text+0xa25): undefined reference to `__chkstk_ms' /mingw/lib/libmingwex.a(glob.o):(.text+0xc15): more undefined references to `__chkstk_ms' follow collect2: ld returned 1 exit status
i ran the command
mingw-get upgrade "mingwer=3.20.*"
and
mingw-get upgrade "mingwer=3.18.*"
but didn't solve the issue
Edit: Command ran
gcc compvisia.c init.c bitboards.c hashkeys.c board.c -o compvisia
Code:
#include "stdio.h"
#include "stdlib.h"
int main() {
printf("hello world");
return 0;
}
Looks like the errors are related to stack protection.
Maybe your system has a mix of different MinGW versions installed, causing linking with mismatching libraries.
MinGW 3 is old, you should really consider moving to MinGW-w64.
You can get a standalone build of MinGW-w64 from https://winlibs.com/ that you can extract somewhere without installing to test if it makes a difference for your source code.
Make sure to use the full path to gcc.exe so you are in fact using the one you think you are using.

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.

Eclipse GCC C Cross Compiling for OpenWrt not working

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.

I compiled the LAPACK and BLAS but my system can not recognize the library

I am working on a project written in a mix of Fortran 90 and Fortran 77 and now need to link the LAPACK/BLAS libraries, from netlib.org, to the project, all in a Linux environment. I used the gfortran compiler flags OPTS = -O2 -fPIC -m64 in the given Makefile, and then made it using
make blaslib
make
And it finished normally, or at least I think so.
Then, I copied the files in /usr/local/lib and /usr/local/bin/ and /usr/local/lib64/
but it didn't work. I even used the option -L/path/to/lapack/liblapack.a and it didn't work also.
When I compile my code, I get the following error:
qrB.o: In function `qrfactorizeb_':
qrB.f90:(.text+0x64f): undefined reference to `zgeqp3_'
collect2: ld returned 1 exit status
make: *** [run] Error 1
I am really unsure what to make of this error. I tested it in 3 other workstations and it didn't help! Can anyone help me?
I had the same problem some time ago! Dual working with Windows and Linux and also ease of playing with options in Windows taught me something interesting!
Try compiling such as:
[...]$
ifort liblapack.a libblas.a libslatec.a *.o -o profmm
and as you know, it means that I want to use 3 libraries to compile and link my files into profmm output file. It has no syntax error, but it leads to a lot of errors like:
preconditioner3.o: In function factorb_':
preconditioner3.f:(.text+0x1add): undefined reference tozgetrf_'
.
.
preconditioner.o: In function factorpre_':
preconditioner.f:(.text+0x13a2): undefined reference tozgetrf_'
preconditioner.f:(.text+0x18bb): undefined reference to zgetri_'
zbesh.o: In functionzbesh_':
zbesh.f:(.text+0xb3): undefined reference to d1mach_'
zbesh.f:(.text+0xcf): undefined reference toi1mach_'
.
.
.
and many more errors indicating that ifort is unable to read my libraries even though they are here in my current directory!
But simply change the command as follow:
[...]$ ifort *.o liblapack.a libblas.a libslatec.a -o profmm
and it works fine with no error! So it means that now ifort can read my library (local ones)! Also note that changing the order of libraries are very important, and it depends on the order of usage of those subroutines inside the program. So always try to reorder the library chain to check for possible errors.
Hope it helps.

"undefined reference to `strnstr'" when installing cminpack

I am installing cminpack 1.1.2 on a clean install of Ubuntu 10.10.
When running sudo make in the cminpack folder, the folloing error occurs at 52%:
[ 52%] Building C object examples/CMakeFiles/genf77tests.dir/genf77tests.c.o
/usr/lib/cminpack-1.1.2/examples/genf77tests.c: In function ‘main’:
/usr/lib/cminpack-1.1.2/examples/genf77tests.c:44: warning: assignment makes pointer from integer without a cast
/usr/lib/cminpack-1.1.2/examples/genf77tests.c:86: warning: comparison between pointer and integer
Linking C executable genf77tests
CMakeFiles/genf77tests.dir/genf77tests.c.o: In function `main':
genf77tests.c:(.text+0xb5): undefined reference to `strnstr'
genf77tests.c:(.text+0x2a9): undefined reference to `strnstr'
collect2: ld returned 1 exit status
make[2]: *** [examples/genf77tests] Error 1
make[1]: *** [examples/CMakeFiles/genf77tests.dir/all] Error 2
make: *** [all] Error 2
I have looked inside genf77tests.c, and #include <string.h> is present, so I don't see why there is a problem finding strnstr.
Is there anything I can do to fix this issue?
strnstr is a non-standard function. Specifically, it is not included in Glibc. Whoever wrote genf77test.c did not test it on Linux/glibc...
strnstr is not standard C; as far as I can see, it's provided on the BSD UNIX family.
Still, if the only problem in your compilation is that one you can provide its prototype1 and add to the files to be compiled one that contains its implementation (for example this one).
Actually, you may not even need it: if the compilation gets to the linking stage and fails just there, you can solve throwing in just the implementation of strnstr.

Resources