Including a static library in a C project (Eclipse) - c

I'm currently developing an application using SDL. In order to utilize it, I have already added the library and header files in the project's settings under C/C++ Build -> Settings -> Tool Settings -> Libraries/Includes. However, when I try to build a test program like
#include <stdio.h>
#include <SDL/SDL.h>
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Quit();
return 0;
}
I get this beautiful error message during the link process:
d:/programme/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../libmingw32.a(main.o): In function main':
C:\MinGW\msys\1.0\src\mingwrt/../mingw/main.c:73: undefined reference toWinMain#16'
Which is rather weird, given that the directory C:\MinGW doesn't even exist at all.
The command used for linking is this one:
gcc "-LD:\Programme\SDL\lib" -o test.exe test.o -lsdl
After two hours of trying to get a library link to work, I'm pretty confused and have no idea what I'm doing wrong. Help would be appreciated.

It looks like you are building a Windows GUI application, which requires a WinMain, while your code only provides a main function which would be for console applications.
So if this is supposed to be a console application, you must adjust your linker settings accordingly, or you must declare a WinMain.

Related

Netbeans - How to add external C libraries? IUP

I'm learning C, and I want to use my first external library.
Firstly I'll show the simplistic code I'm trying to compile:
#include <stdlib.h>
#include <iup.h>
int main(int argc, char **argv)
{
IupOpen(&argc, &argv);
IupMessage("Hello World 1", "Hello world from IUP.");
IupClose();
return EXIT_SUCCESS;
}
The external library being IUP.
I have downloaded the following to my documents folder:
Then I configured Netbeans build options by first adding the header file directory:
Then finally I added the .dll file:
https://prnt.sc/slf1z8
(I added a link because I thought the post was getting too long)
However, while the code no longer shows a red underline under the functions I'm trying to use, the compiler shows "cannot find -liup"
I don't know what could be going wrong. I was following this link tutorial:
http://webserver2.tecgraf.puc-rio.br/iup/en/ide_guide/netbeans.html
Thank you!
When you link, even using a DLL, you need a ".lib" or ".a" file depending on the compiler. In this case this library is called an import library, and contains just "pointers" to the functions in the DLL.
The IUP packages include those import libraries you need. Probably you downloaded package iup-3.29_Win64_dllw6_lib.zip which include the ".a" files you will need.

"undefined reference" error after adding function to a C project

I've added a new function wiringPiVersion() to wiringPi, but after I build and install the shared library, when I attempt to compile a small C program around it, I get:
wpi_ver.c:(.text+0xc): undefined reference to `wiringPiVersion'
However, when I include it in an XS based Perl module, all works well. I don't know enough about C to figure out what's going wrong here, and I've been searching for the better part of two hours trying different things to no avail.
Here's my small C program to test the new function:
#include <stdio.h>
#include <wiringPi.h>
int main (){
char * ver = wiringPiVersion();
printf("wiringPi version: %s\n", ver);
return 0;
}
Compilation that throws the error:
gcc -o ver wpi_ver.c -lwiringPi
The addition to wiringPi's header file:
extern char * wiringPiVersion(void);
The wiringPi's .c file addition:
#define WPI_VERSION "2.36"
char * wiringPiVersion(void){
return WPI_VERSION;
}
In my Perl module's XS file, I have:
char *
wiringPiVersion()
...and my Perl module's Makefile.PL
LIBS => ['-lwiringPi'],
...and after re-installing the Perl module, I can access the function without any issues in a test script.
I'm hoping this is something simple I'm overlooking which someone may be able to point out. My question is, how do I rectify this?
So it turned out that there were two .so files generated when I rebuilt wiringPi... one in the wiringPi's build directory way under my home directory, and the other in /usr/local/lib.
After a tip in comments, I added the library path explicitly:
gcc -o ver wpi_ver.c -L/usr/local/lib -lwiringPi
...and it all fell together and works as expected:
$ ./ver
wiringPi version: 2.36
Note: I have sent Gordon the patch in hopes it gets included in the next wiringPi cut.
Update: I received an email back from Gordon and he stated that currently, only the gpio application has the ability to report the version, so he advised that he's going to add something similar to my patch in a future release.
Although already solved, I added this answer to show what gave me the hint.
Error message "undefined reference" points to a linker error (cf. answer on SO), so its about checking if the correct library is drawn.

Correct command line parameters for gcc compilation of SDL

I recently started SDL2.0 programming.
I did a lot of researches and i tried all but i still get those "undefined reference" errors for all the SDL functions:
undefined reference to `SDL_Init'|
undefined reference to `SDL_GetError'|
undefined reference to `SDL_Quit'|
||=== Build finished: 3 errors, 0 warnings ===|
on that simple test program:
#include <stdlib.h>
#include <stdio.h>
#include "SDL.h"
int main(int argc, char* argv[])
{
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0) {
fprintf(stderr, "\nUnable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
atexit(SDL_Quit);
return 0;
}
If i have to guess the problem occurs due to the wrong command line syntax.
In this case what should be the correct one?
You aren't linking to the SDL libraries correctly.
Add the following lines int Other Linker Option
-lSDL -lSDLmain
mingw32
SDLmain
SDL
Also You need to check setup for how to compile SDL in codeblock
http://wiki.codeblocks.org/index.php?title=Using_SDL_with_Code::Blocks
http://lazyfoo.net/SDL_tutorials/lesson01/windows/codeblocks/
If it's not too late then try from the beginging to how to set up SDL in codeblock and successfully run it? Below link provide you exact steps for it.
http://www.dreamincode.net/forums/topic/57275-setting-up-codeblocks-to-work-with-sdl/
You might have not linked SDL2 correctly to your CodeBlocks project and not referred to SDL2 correctly in your code.
1:
Go to "Linker options" in "Build Options" menu and make sure you have added these library's to your project like this:
Library's to include in linker options
Importent!: save project before running it after adding/changeing library's.
2:
Change:
#include "SDL.h"
to this:
#include <SDL2/SDL.h>
if you still encounter problems compiling and running it, it's most likely either, your SDL2 files not placed correctly in the compiler's folders, or your using an version of gcc with some missing tools.
These Youtube video's explain everything in great detail:
1: https://www.youtube.com/watch?v=x0LUf7Ibpi0
2: https://www.youtube.com/watch?v=EtUw_7CvRRo

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.

how to test a static library project with codeblocks and target definition

I have a static library project and now i want to test some functions. To achive my goal i added a new target of type console, because when i try to "run" my library i get right the message "...select an host application to run...", then i added to this target a test file, test.c, that use some function.
From my knowledge, targets are different way to process sources file, so the release target produces a *.a file, and the debug target produces other.
// test.c
#include <stdio.h>
struct object_geometry *load_ObjModel(char *fileName);
int main()
{
printf("Buongiorno!");
load_ObjModel("../dado.obj");
return 0;
}
After writing test.c, i select the last created target and during debugging something doesn't work, because i can't use the modality "go line by line", or putting breakpoint in the above code, so:
how can i debug my library, without the creation fo a new project?
what's a target in codeblocks?
(other useful related informations are appreciated)

Resources