"First defined here" error in Eclipse with C - c

I'm trying to compile a C program using libxml2 in Eclipse. It seems like my code doesn't have problems, but there are errors when I build my project.
The error output is in this screenshot: https://drive.google.com/file/d/0BwV-0_2diIaaQlZHM2Fwa2R0LWc/edit
Before this error, I had an “Undefined reference to” error, but it was because I forgot to link the library libxml2. Now it's the problem in the screenshot. I don't what to do.
[EDITED]
I solved my problem I just need to put -nostartfiles in the linker flags.

I solved my problem I just need to write -nostartfiles in the Linker Flags box :D
To find "Linker Flags" box go to Your Project > Properties > C/C++Build > Settings > GCC C Linker > Miscellaneous
That's it.
Thanks for help.

I don't think your solution is acceptable, I think it will lead to the same problem when your project is executed in other environment (another eclipse configuration, running it from console, etc)
That error occurs when you have defined the same function more than one time in your project, and I bet it's because you have defined the same function you already had in a library.
For example, if I have lib1.h with a function called hello() and then write the same function in your main.c (having that library linked) that problem will occur.
In fact, in your image, I can see "multiple definition of __data_start"
So I think you just have to change the problematic function name and it will be fixed.
In addition, this problem will be caused if you include the same library more than once in your program, but it can be solved using preprocessor (if you are interested in this, google it, since it would be off-topic and make my answer too long)

Related

Confused by "undefined reference" linker error, and inability to find culprit object file

I have a large, mixed (C, C++) workspace with a complex make file layout. I'm porting in a new library, and working through compilation issues. I have a GNU linker error (in what I am pretty sure is entirely sourced from C code in this instance) of the form:
path/to/object_file.o: In function `function_name':
(.text+0x0x11e4a0): undefined reference to `missing_function'
Now, it is my expectation that if I execute the "strings" command on object_file.o, I should see reference to both function_name and missing_function. I find neither. If I peruse the output of "strings," I do see other symbols that I recognize, so it is not mangling everything.
Is it possible that the linker error message is inaccurate?
I did find the command which builds object_file.o. It is another linker of a bunch of .a files. I executed "strings" on each of them, and did not find function_name in any of them.
So, I am trying to find the build process steps that lead up to this error, as I am not sure that function_name is relevant in this linking step. Any tips on how to do this are appreciated.

Undefined reference to the function 'check' (GCC compilation in MinGW)

I seem to be getting the rookie error where it says, undefined reference to 'check', as shown below:
This should not be a problem, as I have in fact made a check.h and included in hiker.c, as shown below:
Does anybody know the source of this problem? I have just started using MinGW(as I wanted to learn programming C on Windows).
Here is a picture of the main function. I can add the code too if necessary:
I guess that check function is implemented in a file check.c
You must link that file also, because of your check.h export the prototype to let the compiler know how the check function is structured, but the linker needs the check function code compiled and reachable.
What you need is to compile using a command like this:
gcc -Wall hiker.c check.c -o hiker.exe
Take also note that linker is giving you another error about WinMain#16
This means that you started a windows application project, I guess you must change your project to console project type.

Ubuntu libdispatch

I am trying to port a program that uses GCD (Grand Central Dispatch) from OSX to Ubuntu 11.10. I installed libdispatch but I keep getting the following error:
undefined reference to dispatch_main()
The strange thing is that dispatch_main() is declared in a header file that I include and I call other functions declared in that header file and the compiler recognizes them. It is only dispatch_main() that it cannot see and if I call dispatch_main(2) it says that there are too many arguments, so I know the compiler can see the header.
I tried separating the compile and link steps (clang -c...) since that worked for an undefined reference error before, but it doesn't seem to do anything here...
Anybody have any suggestions? I'm pretty stumped on this one...
It sounds like you are missing the library from your link line. When you compile your program into an executable, add the library to the command. I am guessing it should look something like this:
clang x.c y.c z.c -ldispatch

Finding an absolute path to a folder in Cygwin

I am running a Eclipse for C/C++ using cygwin GCC. I need to get the library path of "libws2_32.a", which is in C:\cygwin\lib\w32api for me in windows. However I do not understand how this is translated into a cygwin-path.
I've tried stuff like:
/cygdrive/c/cygwin/lib/w32api
/lib/w32api
/usr/lib/w32api
Any ideas?
EDIT: update
Here's the make Eclipse log used for building, if it helps:
make all
Building target: Filesharing_core.dll
Invoking: Cygwin C Linker
gcc -L/cygdrive/c/cygwin/lib/w32api -shared -o"Filesharing_core.dll" ./src/test.o -llibws2_32.a
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: cannot find -llibws2_32.a
collect2: ld returned 1 exit status
make: *** [Filesharing_core.dll] Error 1
It's not a problem with the path; it's a problem with the name of the library (the -l option).
Where you have entered libws2_32.a, instead enter ws2_32.
See the GCC manual for how the -l option works.
Generally, when asking for help with programming, don't describe, but show. Don't describe what you're doing; don't describe what you think the error is. Instead, show exactly what you're doing, show the exact code (or a cut down version that exhibits the same problem), and show the exact error message.
This way others don't have to guess as to what's wrong, and you are more likely to receive an answer right away that solves your problem.
You can use the find utility to find the file, although it may take a while.
find / -name libws2_32.a -print
Edit: Based on your edit to the question, simply move -llibws2_32.a to before ./src/test.o.
The folder C:\Opt\Cygwin\lib\w32api or whatever it is in your system is simply /lib/w32api from within Cygwin. The library you're looking for is /lib/w32api/libws2_32.a.
But I'm not sure what you're trying to achieve. I guess your Eclipse in running in Windows, and not from a Cygwin'ized version of Java? Why are you using Cygwin GCC? Do you want to compile for Cygwin? If not, MinGW or TDM-GCC would be the tool to pick.
If you do want to compile for the Cygwin environment using Eclipse (a combination I haven't tried), then user experience like the following might be helpful:
http://www.benjaminarai.com/benjamin_arai/index.php?display=/eclipsecygwingcc.php
(I simply googled: eclipse cygwin gcc)

Porting NewLib for my OS: some questions

I am trying to port NewLib for my OS (I am following this tutorial: http://wiki.osdev.org/Porting_Newlib), and I have some questions.
Once LibGloss is done and compiled, when exactly will I have to use the libnosys.a that have been created? Is it when I will compile my main.c?
mipsel-uknown-elf-gcc main.c -Llibnosys.a`
My crt0.c is done. And I have to "link it as the first object". How can I do that? Is it something like this?
mipsel-uknown-elf-ld crt0.o main.o
Thanks for your answers!
Linking as the first object might work just fine like you are displaying, but the docs does mention using a linker script and adding crt0.o as STARTUP() -- I'm not too familiar with linker scripts, but you can find the default linker script and possibly create it/adjust it:
Syntax of linking script: http://wiki.osdev.org/Linker_Scripts
http://sourceware.org/binutils/docs-2.19/ld/Scripts.html#Scripts
The linker always uses a linker script. If you do not supply one yourself, the linker
will use a default script that is compiled into the linker executable. You can use the
`--verbose' command line option to display the default linker script. Certain command
line options, such as `-r' or `-N', will affect the default linker script.
The same can probably be done with other system libraries that always have to be part of the linking.
It's fine to add all on the command line, but a bit tedious in the end.
Are you getting any errors or wrong results since you are asking or what?

Resources