Eclipse with MinGW does not link correctly - c

I use the Eclipse Luna IDE for C/C++ (CDT) and MinGW for programming C with Microsoft Windows 7.
I try to write a simple program which uses KissFFT
This is not directly a library, this are only some *.c and *.h-Files.
My example program is stored at E:\Programming\Programs\Simple_FFT\
That is my example program:
#include <stdio.h>
#include <stdlib.h>
#include <tools/kiss_fftr.h>
int nfft = 1024;
int inverse_fft = 0;
int main(void) {
kiss_fftr_cfg cfg = kiss_fftr_alloc(nfft, inverse_fft, NULL, NULL);
kiss_fft_scalar timedata[nfft]; //input
kiss_fft_cpx freqdata[nfft]; //output
while (1)
{
timedata[nfft] = 0;
kiss_fftr(cfg, timedata, freqdata);
free(cfg);
}
return 0;
}
As you can see I included kiss_fftr.h.
This file on the other hand is including kiss_fft.h.
In Eclipse, I went to Properties -> C/C++ General -> Paths and Symbols -> Includes -> GNU C -> and there i added the directorys E:\Programming\Libraries\kiss_fft130 and E:\Programming\Libraries\kiss_fft130\tools
Because this are the folders where the needed header files and c files are stored.
So everything fine, not?
But now the curious thing: I can't compile the program with Eclipse. I'm relatively new to programming but as far as I know, if I include a header file and the directory is included in Eclipse, the related *.c-file should be linked automatically.
Instead I get undefinded references:
src\Simple_FFT.o:Simple_FFT.c:(.text.startup+0x3b): undefined reference to `kiss_fftr_alloc'
src\Simple_FFT.o:Simple_FFT.c:(.text.startup+0x86): undefined reference to `kiss_fftr'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: src\Simple_FFT.o: bad reloc address 0x20 in section `.eh_frame'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
The problem is, that Eclipse is not linking the c-files of KissFFT to the included headers.
So I found two solutions to bypass this problem:
First: Compile the program manually:
gcc -o E:\Programming\Programs\Simple_FFT\Release\Simple_FFT -I"E:\Programming\Libraries\kiss_fft130" -I"E:\Programming\Libraries\kiss_fft130\tools" E:\Programming\Programs\Simple_FFT\src\Simple_FFT.c E:\Programming\Libraries\kiss_fft130\tools\kiss_fftr.c E:\Programming\Libraries\kiss_fft130\kiss_fft.c
As you can see, I included the *.c-Files "kiss_fftr and kiss_fft" manually and compilation is fine.
Second solution: In Eclipse, I went to Project -> Properties -> C/C++ Build -> Settings -> Tool Settings -> MinGW C Linker -> Miscellaneous -> Other Objects -> and added E:\Programming\Libraries\kiss_fft130\kiss_fftr.c and E:\Programming\Libraries\kiss_fft130\kiss_fft.c.
Then the program is compiling fine, too.
Both is not my intention. Because I would have to manually link every *.c-file I use in my program. What is my failure in the properties of Eclipse, that it does not automatically link the correct files together?
Would be very happy to get an answer so I can start programming correctly :)

You need to add your C files folders to the Source Locations in the Project Properties->C/C++ General->Paths and Symbols. Or, if you have your kiss library compiled (.lib or .a) you should add it in the linker section Library Search path and Libraries

Related

Include Files in Segger Embedded Studio

I am just starting out with Segger Embedded Studio. Right now trying to call a function of a included header file. The file seems to be included since it shows up in the dependencies. For now I simply included the header and C-File in the project directory.
The included Header- and C-Files are simply:
//##### Header-File ######
#ifndef TEST_H_
#define TEST_H_
void printText(void);
#endif //TEST_H_
and:
//###### C-File #########
#include <test.h>
#include <stdio.h>
#include <stdlib.h>
void printText(void)
{
printf("Hello");
}
But when I try to call the printText Function in my main I get the error:
"Undefined Symbol: printText".
Why is the function not recognized?
"Undefined symbol" is a linker error. You are not linking the object code containing the definition of printText().
It is not an issue with the header file; including a header file does not cause the associated code to be linked - that is just the declaration so the compiler knows what the interface looks like. It is the linker than combines the separately compiled object code to form a program. You have not told the linker to use the object code containing printText(), and you have not told your IDE project to compile it to generate that object code.
The project tree clearly shows that only main.c is included in your project; you need to add the C file containing printText() too.
The concept of separate compilation and linking is what you need to grasp here.
Thank you Clifford for your answer. You are right I had some miss-conceptions about which files will be linked while building the project. In the special case of segger embedded studio there are, as I know by now, two ways to reference extern files.
Adding the files to the sources files folder is straight forward, but must be done separately for each project you want to use the respective files.
For frequently used files it is beneficial to create a dedicated library solution. Such a library can then be imported to any solution by choosing "add existing project". This will add all files of the library to your current solution (and show them in the project-tree). Now click the tab project -> dependencies. Your library should show up here. By acitivating the check box the linker will compile the referenced project upon builing your solution, allowing for the usage of your library functions.
Adding and linking library-project to a current solution

What is the function of libgcc_s_dw2-1.dll?

I wrote the following program in Codeblocks
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b, c;
a = 5;
b = 6;
c = a+b;
printf("Hello world!\n");
return 0;
}
for both debug and release builds. The exe files are created in the respective bin\debug and bin\release folders, and the program works fine when run from codeblocks.
However when I try to execute the exe files form bin\debug or bin\release, I get the libgcc_s_dw2-1.dll is missing from your computer. error.
Searching for libgcc_s_dw2-1.dll only brings out other posts that ask how to fix this problem, where the suggested solution is to either add the path to libgcc_s_dw2-1.dll, which is somewhere inside inthe MinGW installation, to the system path, or statically link the dll file to the program during compilation.
My question is, why is this necessary? This is a very simple hello world program. The file is in a codeblocks project and file extension is .c (main.c, so I'm guessing gcc is automatically invoking the C compiler and it doesn't have anything to do with compiling C code with cpp compiler?) and the compiler settings have been left at their default values after a fresh MinGW and Codeblocks install. Since this program is very simple I am assuming it should work the same way in any other older version compilers as well.
Then what is the use of this dll file? What is happening under the hood when compiling this program? Suppose I compile the hello world program above and distribute the exe file to my friends. How am I supposed to know that I should have statically linked the dll file during compile?

Open new C project with eclipse for new developer

I want to start learning C and download Eclipse for C\C++ and want to start new project.
I found this tutorial and after create new project, choose C project with MinGcc and hit finish this is the errors i got:
Symbol 'EXIT_SUCCESS' could not be resolved
What i am doing wrong ?
EXIT_SUCCESS is defined in stdlib.h:
#include <stdlib.h>
int main(void)
{
return EXIT_SUCCESS;
}
Based on your comments, your problem appears to be with Eclipse. You might want to try off the main menu:
Project->C/C++ Index->
Freshen All Files
Update with Modified Files
Re-resolve Unresolved Includes
Search for Unresolved Includes
In the Project Exlorer tree, it lists where Eclipse CDT looks for include files. You should see a directory in there that holds stdlib.h. On my machine it is in c:\MinGW\include.

Unable to link libpng or zlib in Eclipse with MinGW C linker

I'm new to external static libraries in C, and i'm having trouble adding pnglib (or any library) to Eclipse. Im using Eclipse v3.3.2 with mingw on windows 7 64bit.
I first followed these instructions to install libpng and zlib: http://wiki.openttd.org/Compiling_on_MinGW
Then in Eclipse under C/C++ Build -> Settings ->Tool Settings -> MinGW C Linker -> Libraries
I added: "png" then "z" in Libraries (-l)
and: "C:\MinGW\libpng-1.5.12" then "C:\MinGW\zlib-1.2.7" in the Library search path (-L)
If I execute the simplest code:
#include <stdio.h>
#include <zlib.h>
#include <png.h>
int main(void) {
printf("foo\n");
unsigned char header[8];
//png_sig_cmp(header, 0, 0);
return 0;
}
It works fine, however as soon as i uncomment the function, the code compiles (without error/warning), but does absolutely nothing, (not even the print statement). This happens when I use ANY function from an external library.
I assume it can read the headers but there's funny business with finding function definitions.
I have no idea where i went wrong.
I'm sure I have missed something trivial!

Undefined references when compiling gSOAP client

I'm trying to create a client for a web service in C. I was generated C files with the wsdl2h and soapcpp2. In netbeans I'm added the generated files and the gSOAP include dir to the project's include directories.
my main file looks like this:
#include <stdio.h>
#include <stdlib.h>
#include <soapH.h>
#include <webserviceSoap12.nsmap>
int main(int argc, char** argv) {
struct soap *soap1 = soap_new();
struct _ns1__getAllCustomer *quote;
struct _ns1__getAllCustomerResponse *quote2;
if (soap_call___ns2__getAllCustomer(soap1, NULL, NULL, quote, quote2) == SOAP_OK)
printf("asd\n");
else // an error occurred
soap_print_fault(soap1, stderr); // display the SOAP fault on the stderr stream
return (EXIT_SUCCESS);
}
I copied the most of this from the gSOAP website's getting started section.
When I try to compile i get the following error:
build/Debug/MinGW-Windows/main.o: In function `main':
\NetBeansProjects\WebServiceClient/main.c:19: undefined reference to `soap_new_LIBRARY_VERSION_REQUIRED_20808'
\NetBeansProjects\WebServiceClient/main.c:22: undefined reference to `soap_call___ns2__getAllCustomer'
\NetBeansProjects\WebServiceClient/main.c:25: undefined reference to `soap_print_fault'
If I add the "soapC.c" "soapClient.c" "soapClientLib.c" files to the project I get a bunch of more undefinied reference.
What am I doing wrong? I'm using Netbeans ide with MinGW compiler on Win7. What other libraries I need or what other files should I include?
I managed to solve the problem by adding the files "soapC.c" "soapClient.c" "stdsoap.c" to the project files and in the Project propertie - Include Directories adding the files generated by soapcpp2 and the gSOAP toolkit's gsoap directory
You will need to link in the proper libraries. You will need to add the appropriate libraries using the -l switch and you will optionally need to pass the path to where these libraries reside via -L. Also, note that the libraries ending with a ++ are typically the ones you should use if you're using C++. So, your command line shoulde include at least:
For C:
gcc ... -lgsoap -L/path/to/gsoap/binaries
For C++:
g++ ... -lgsoap++ -L/path/to/gsoap/binaries
Also, depending on whether you're using additional features such as SSL, cookies etc. you will need to link these libraries in too:
g++ ... -lgsoap++ -lgsoapssl++ -L/path/...
If you're using a different toolchain, lookup the documentation for the exact switches.
I had this problem in Debian BullsEye (11), -lgsoap++ is necessary and it was solved when I added /gsoap_library_path/libgsoap++.a to g++ compiler command line.

Resources