How to compile an application using libcurl with gcc on mingw - c

I am trying to compile a C file containing this code :
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
int main()
{
curl_global_init( CURL_GLOBAL_ALL );
CURL * myHandle;
CURLcode result; // We’ll store the result of CURL’s webpage retrieval, for simple error checking.
myHandle = curl_easy_init ( ) ;
// Notice the lack of major error checking, for brevity
curl_easy_setopt(myHandle, CURLOPT_URL, "http://www.example.com");
result = curl_easy_perform( myHandle );
curl_easy_cleanup( myHandle );
printf("LibCurl rules!\n");
return 0;
}
,
Well when I try to compile :
gcc url.c -lcurl
I get the following errors:
F:\MinGW\home>gcc url.c -lcurl
C:\Users\Ehsan\AppData\Local\Temp\ccF7doFN.o:url.c:(.text+0x8e): undefined reference to `_imp__curl_global_init'
C:\Users\Ehsan\AppData\Local\Temp\ccF7doFN.o:url.c:(.text+0x95): undefined reference to `_imp__curl_easy_init'
C:\Users\Ehsan\AppData\Local\Temp\ccF7doFN.o:url.c:(.text+0xbf): undefined reference to `_imp__curl_easy_setopt'
C:\Users\Ehsan\AppData\Local\Temp\ccF7doFN.o:url.c:(.text+0xcd): undefined reference to `_imp__curl_easy_perform'
C:\Users\Ehsan\AppData\Local\Temp\ccF7doFN.o:url.c:(.text+0xdf): undefined reference to `_imp__curl_easy_cleanup'
collect2.exe: error: ld returned 1 exit status
I downloaded this distribution
and copied all files to bin, include , and lib folder.
What am I missing?
Update
Well I fixed the problem by using -lcurldll.
In my lib folder I have both libcurl.a and libcurldll.a.
Why linking with libcurl.a can't compile but with libcurldll.a it works fine?

You can check the lib name by:
readelf -d [LIB_PATH]
That would solve you the problem.

Related

Error while linking main file with my header file : undefined reference to `myHeaderFunction'

I am trying to make my first header file in C.
There are three files in same directory.
myHeaderFile.h
// It is a good practise to only declare function in header file.
int myHeaderFunction();
myHeaderFile.c
#include "myHeaderFile.h"
int myHeaderFunction()
{
return 1;
}
main.c
#include<stdio.h>
#include"myHeaderFile.h"
int main()
{
printf("main function.\n");
int output;
output = myHeaderFunction();
printf("%d\n",output);
return 0;
}
Cronology:
I created the first file myHeaderFile.h and saved it.
Then I created myHeaderFile.h and saved it.
Then, I created main.h and saved it. (I didn't run any of the file above'
I opened console and wrote the command as written below.
Then, I run main.h and it gave the error as mentioned below.
I open the console in the same directory and type:
gcc -Wall -o combined main.c myHeaderFile.c
It creates a file named combined.exe
And then I run main.c and it gives an error
undefined reference to `myHeaderFunction'
collect2.exe: error: ld returned 1 exit status
There are few answers for the error
undefined reference to `myHeaderFunction' but it involves too many function and files and gcc commands that I didn't understood as a begineer.
So I request not to close this answer and give a solution specific to this problem.

Compiling C programs with static files

I am trying to compile a c program with a static library and its not working .
This is the error :
undefined reference to `calculatearea'
collect2.exe: error: ld returned 1 exit status .
The static files were made with the gcc / g++ compilers .
This is the main code :
#include <stdio.h>
#include <stdint.h>
int calculatearea(int a , int b);
int main()
{
int c = calculatearea(2,4);
printf("%d",c);
getchar();
return 0;
}
edit :
: screenshot of compiler error
From the above code we can see that you have declared the function int calculatearea(int a , int b); but have not written any definition for the same. and you are calling this function in the main. compiler is not finding the definition for the function calculatearea and giving error.
To solve this:
1) Write the definition for function calculatearea in the same file.
2) Make use of extern specifier with this function declaration and make sure that definition is present with the link library at the time of compilation.
3) As mentioned in the picture if the area.o have the definition of function calculatearea, then compile as below, this will generate a.out in linux:
gcc filename.c area.o

Function pointer pointing to built in function in C

I'm trying to set a function pointer to point to the pow function.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void){
double (*func)(double, double) = pow;
return 0;
}
But the program doesn't compile.
I get this error:
$ gcc test.c -o test
/tmp/ccD6Pmmn.o: In function `main':
test.c:(.text+0x8): undefined reference to `pow'
collect2: error: ld returned 1 exit status
I'm using Ubuntu 15.10.
Anyone knows what's wrong with my code?
Thanks
You need to compile with -lm via command line or configure your IDE to add it into the linking process. This is due to the fact that some libraries are pretty large and to avoid taking up space in your program and compilation time, this was setup at the beginning of C when computers were much slower and would take MUCH more to compile and a matter of space was CRUCIAL.

working with acl.h - undefined reference to `acl_init' error

I want write simple C program to set ACL to one particular file on Linux. My starting code is trying to use function from "acl.h". I'm using Ubuntu 13.10, I've installed "Access control list utilities" - "acl 2.2.52-1". Here is my code:
#include <sys/acl.h>
#include <stdio.h>
int main () {
acl_t aclvar;
int count = 1;
aclvar = acl_init(count);
return 0;
}
The problem is, that I get error while compiling with "gcc myAcl.c" or "gcc -lacl myAcl.c":
/tmp/cc5sVzSR.o: In function `main':
myAcl.c:(.text+0x15): undefined reference to `acl_init'
collect2: error: ld returned 1 exit status
How can I resolve this error?
The libraries you link to needs to come last
gcc myAcl.c -lacl

Link errors using FANN

I'm trying to build a basic FANN (Fast Artificial Neural Network) project on Windows with MinGW. However, whenever I try to link the executable, I run into a bunch of undefined reference to errors. Interestingly, if I don't link the library at all, I get more errors, implying that at least some of the library is working. The code for the file I'm trying to compile and link is:
#include "doublefann.h"
int main() {
const unsigned int num_input_neurons = 9;
const unsigned int num_output_neurons = 1;
const unsigned int num_layers = 3;
const unsigned int num_hidden_neurons = 9;
const float desired_error = (const float) 0;
const unsigned int max_epochs = 500000;
const unsigned int epochs_between_reports = 1000;
struct fann *ann = fann_create_standard(num_layers,
num_input_neurons,
num_hidden_neurons,
num_output_neurons);
fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC);
fann_train_on_file(ann,
"titanic-training.data",
max_epochs,
epochs_between_reports,
desired_error);
fann_save(ann, "titanic.net");
fann_destroy(ann);
return 0;
}
and the command I'm using to compile and link is:
gcc -Wall -Ifann\src\include titanic-train.c -Lfann\bin -lfanndouble -o titanic-train.exe
The errors I'm getting back are:
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0x7f): undefined reference to `fann_set_activation_function_hidden'
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0x93): undefined reference to `fann_set_activation_function_output'
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0xbf): undefined reference to `fann_train_on_file'
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0xd3): undefined reference to `fann_save'
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0xdf): undefined reference to `fann_destroy'
c:/fragileprograms/mingw-native/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o: bad reloc address 0x64 in section `.rdata'
c:/fragileprograms/mingw-native/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
If I don't link the library at all, I instead get:
C:\Users\kunkelwe\AppData\Local\Temp\ccyOO3jL.o:titanic-train.c:(.text+0x67): undefined reference to `fann_create_standard'
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0x7f): undefined reference to `fann_set_activation_function_hidden'
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0x93): undefined reference to `fann_set_activation_function_output'
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0xbf): undefined reference to `fann_train_on_file'
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0xd3): undefined reference to `fann_save'
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0xdf): undefined reference to `fann_destroy'
c:/fragileprograms/mingw-native/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o: bad reloc address 0x64 in section `.rdata'
c:/fragileprograms/mingw-native/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
Edit:
As per Haroogan's request, I ran nm fanndouble.lib. The output is rather extensive, so rather than paste it all here, I've made it available via pastebin here: http://pastebin.com/raw.php?i=vybFhEcX
I'm not familiar with nm, but it appears that the missing symbols do exist in the file.
Edit #2:
The contents of doublefann.h are: http://pastebin.com/mrHKJi8C
and the contents of fann.h, which it includes are: http://pastebin.com/gTrHCYAg
Could the problem just be solved by recompiling the library with MinGW?
Edit #3:
Making the changes that Haroogan suggested worked! In addition to those changes, I had to modify the CMakeLists.txt file for FANN by adding:
if (WIN32)
ADD_DEFINITIONS(-DFANN_DLL_EXPORTS)
endif (WIN32)
Then, running cmake -G "MinGW Makefiles" and then mingw32-make in the root of the project produced a file, libdoublefann.dll, that when linked against and included in the directory of the .exe, allowed me, finally, to run my program.
In doublefann.h on the line #116:
#if (_MSC_VER > 1300)
change to:
#if (_MSC_VER > 1300) || defined(__MINGW32__) || defined(__MINGW64__)
Furthermore, on the line #121:
#if defined(_MSC_VER) && (defined(FANN_USE_DLL) || defined(FANN_DLL_EXPORTS))
change to:
#if (defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)) && \
(defined(FANN_USE_DLL) || defined(FANN_DLL_EXPORTS))

Resources