Nearly solved my issue with using libcurl in dev C++, one final hitch. (libcurl.dll is missing) - c

I previously asked a question concerning libcurl. I've managed to find an answer to it on my own, and will be glad to share how I solved the issue, as soon as I'm sure it worked. Here's the thing, previous to this, the linker would throw a fit when I tried to compile, and with a bit of searching and trial and error, I found that including this:
-I curl\include
-L curl\lib
-lws2_32
-lwldap32
into the linker made sure that the program compiled.
The issue is, that once the program compiles, running it returns the error:
The program can't start because libcurl.dll is missing from your computer. Try reinstalling the program to fix this problem.
Here's my code, though I doubt its relevant:
#define CURL_STATICLIB
#include "curl/curl.h"
#include <stdio.h>
#include <stdlib.h>
int main(void) {
CURLcode ret;
CURL *curl = curl_easy_init();
if (curl == NULL) {
fprintf(stderr, "Failed creating CURL easy handle!\n");
exit(EXIT_FAILURE);
}
/*Attempt to get Facebook*/
ret = curl_easy_setopt(curl, CURLOPT_URL, "http://www.facebook.com");
if (ret != CURLE_OK) {
fprintf(stderr, "Failed getting http://www.google.com: %s\n",
curl_easy_strerror(ret));
exit(EXIT_FAILURE);
}
ret = curl_easy_perform(curl);
if (ret != 0) {
fprintf(stderr, "Failed getting http://www.google.com: %s\n",
curl_easy_strerror(ret));
exit(EXIT_FAILURE);
}
return 0;
}
Any help would be appreciated.

You have succesfully linked the compiler to the static library of libcurl. However when you execute the program It tries to find the .ddl for libcurl and is unable to find it at the directories it searches. Usually this is the directory you run the application from. Try putting the .dll at that location and see if that helps.

Related

compiling c language program with libcurl on windows

I am trying to compile a simple c program on windows 10 using gcc from the libcurl website I cloned vcpkg and then ran the .bat file , next I installed curl with the command vcpkg install curl and got this output
Computing installation plan...
The following packages are already installed:
curl[core,non-http,openssl,schannel,ssl,sspi]:x86-windows -> 7.80.0
Package curl:x86-windows is already installed
Restored 0 packages from C:\Users\<me>\AppData\Local\vcpkg\archives in 138.1 us. Use --debug to see more details.
Total elapsed time: 386.9 ms
The package curl provides CMake targets:
find_package(CURL CONFIG REQUIRED)
target_link_libraries(main PRIVATE CURL::libcurl)
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
int report()
{
CURL* curl;
CURLcode res;
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
if (curl) {
/* First set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
data. */
curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.1.12:8000");
const char* c = const_cast<char*>(output.c_str());
printf ("%s", c);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, c );
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
int main()
{
report();
return 0;
}
Now when I try to compile the code above I get the error
c_example_curl.c:3:23: fatal error: curl/curl.h: No such file or directory
#include <curl/curl.h>
^
compilation terminated.
I am compiling using the command
gcc -lcurl file.c
I also tried using the command
gcc -lcurl -I F:\_C_\vcpkg\installed\x86-windows\include\curl file.c
doesn't seem to change anything
I even tried to copy the curl header file to the working directory of the above code but I was not lucky
How do you install libcurl for windows isn't there any command equivalent to the linux one
download curl for windows here.
unpack the zip and place it anywhere on the system (i personally prefer C:\curl).
then compile your script with: gcc file.c -I<path-to-curl> -L<path-to-curl>\lib -lcurl
NOTE: replace <path-to-curl> with the curl location on the system, for example C:\curl

Using curl in C on Windows

I have now followed multiple "guides" on how to use curl in a C project on windows with little to no success. I have installed and build a static build of curl using vs-code and I end up in with the following problem, when I build the following code:
#define CURL_STATICLIB
#include <stdio.h>
#include "curl/curl.h"
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
I get:
>gcc libcurl_test.c
C:\Users\JAKOBV~1.SOF\AppData\Local\Temp\ccsyNorR.o:libcurl_test.c:(.text+0xc3): undefined reference to `curl_easy_init'
C:\Users\JAKOBV~1.SOF\AppData\Local\Temp\ccsyNorR.o:libcurl_test.c:(.text+0xf6): undefined reference to `curl_easy_setopt'
C:\Users\JAKOBV~1.SOF\AppData\Local\Temp\ccsyNorR.o:libcurl_test.c:(.text+0x11a): undefined reference to `curl_easy_setopt'
C:\Users\JAKOBV~1.SOF\AppData\Local\Temp\ccsyNorR.o:libcurl_test.c:(.text+0x126): undefined reference to `curl_easy_perform'
C:\Users\JAKOBV~1.SOF\AppData\Local\Temp\ccsyNorR.o:libcurl_test.c:(.text+0x13d): undefined reference to `curl_easy_strerror'
C:\Users\JAKOBV~1.SOF\AppData\Local\Temp\ccsyNorR.o:libcurl_test.c:(.text+0x165): undefined reference to `curl_easy_cleanup'
collect2.exe: error: ld returned 1 exit status
I saw some that solved the problem using gcc libcurl_test.c -lcurl however then all I get is c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lcurl
I am quite unexperienced when it comes to using C and especially libraries, what am I doing wrong? My file structure is as follows:
And inside the curl folder is the following files:
Please leave a comment if I should add more information, I am not sure what is of interest and what is not....
Merely including curl/curl.h lets the compiler know what functions exist, but it doesn't actually add those functions to the executable. For that, you also need to link with the library that contains the actual implementation of these functions, namely curl/libcurl_a.lib.
To tell gcc to link with libcurl_a.lib, because it is a static library, just put its path on the command line:
gcc libcurl_test.c curl/libcurl_a.lib

why I get "linker command failed with exit code 1 (use -v to see invocation)" error when I compile a cURL lib code?

I try to activate cURL lib on my mac, then I want to check if it works or not...
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://google.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
but i get an error when I compile it...
"linker command failed with exit code 1 (use -v to see invocation)"
i'd be grateful if somebody can help to fix it... cause I'm kinda beginner and it's my first experience with mac and cURL library...
Try linking libcurl library in CMakeLists.txt.
Helped me:
include_directories(/usr/local/opt/curl/include/curl)
target_link_libraries(ProkectName "/usr/local/opt/curl/include/curl/curl.h")

How to trigger a kernel module from shell?

I am using Ubuntu with VirtualBox.
I am defining a new command to my shell to output some characteristics(like sibling tree etc.) of child processes. In order to output these characteristics, I created a kernel module and used task_struct. I also tested my kernel module outside of my shell and it works.
Now my problem is how to trigger this kernel module inside my shell(in C code) so that my kernel module will be loaded?
I searched and find that I need to use system calls like modprobe or insmod but did not understand how to use them. I tried the code below, but it did not work:
setuid(0);
system("/sbin/insmod /.../mymodule.ko");
Thank you for your help.
Loading module using system()
You are trying to become root in your application (by executing setuid(0)), but you don't have permissions to do that (if you run your program as regular user). Instead, you should check if your program was run from root (using getuid()). Also, it's good idea to test if your module file exists at all. Here is an example of such code (it's tested and does all checking needed):
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#define ROOT_UID 0
#define INSMOD_PATH "/sbin/insmod"
#define MOD_PATH "/.../mymodule.ko"
int main(void)
{
uid_t uid;
int res;
/* Check if program being run by root */
uid = getuid();
if (uid != ROOT_UID) {
fprintf(stderr, "Error: Please run this program as root\n");
return EXIT_FAILURE;
}
/* Check if module file exists */
if (access(MOD_PATH, F_OK) == -1) {
fprintf(stderr, "Error: File \"%s\" doesn't exist\n", MOD_PATH);
return EXIT_FAILURE;
}
/* Load module */
res = system(INSMOD_PATH " " MOD_PATH);
if (res != 0) {
fprintf(stderr, "Error loading module: %d\n", res);
return EXIT_FAILURE;
}
printf("Module \"%s\" was successfully loaded\n", MOD_PATH);
return EXIT_SUCCESS;
}
Save this code as main.c file. Be sure to replace MOD_PATH definition with actual path of your module file.
Compile it using next command:
$ gcc -Wall -O2 main.c -o load_module
Now do the next:
$ su
# ./load_module
First command switches your user to root (you will be asked to enter root password). If you don't know root password, try using sudo -s command instead of su.
Second command runs your program.
Pay your attention to the last character at the command prompt:
# means you have root permissions at this point
$ means you only have regular user permissions.
Loading module using finit_module()
Using system() function in C is usually considered a bad practice (because it takes a lot of time for execution and basically just trying to replace a much more simple Bash script).
If you want to load kernel module in C without using system(), you can look into source code of insmod tool. See libkmod/libkmod-module.c file, kmod_module_insert_module() function. You can see those sources here.
Pay attention to finit_module() function call. A good explanation about this system call can be found at manual pages:
$ man finit_module
Here is an example how you can use finit_module() system call:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <sys/stat.h>
#include <fcntl.h>
#define ROOT_UID 0
#define MOD_PATH "/.../mymodule.ko"
static inline int finit_module(int fd, const char *uargs, int flags)
{
return syscall(__NR_finit_module, fd, uargs, flags);
}
int main(void)
{
uid_t uid;
long res;
int fd;
/* Check if program being run by root */
uid = getuid();
if (uid != ROOT_UID) {
fprintf(stderr, "Error: Please run this program as root\n");
return EXIT_FAILURE;
}
/* Check if module file exists */
if (access(MOD_PATH, F_OK) == -1) {
fprintf(stderr, "Error: File \"%s\" doesn't exist\n", MOD_PATH);
return EXIT_FAILURE;
}
/* Load module */
fd = open(MOD_PATH, O_RDONLY | O_CLOEXEC);
if (fd < 0) {
perror("Unable to open module file");
return EXIT_FAILURE;
}
res = finit_module(fd, "", 0);
if (res != 0) {
perror("Error when loading module");
close(fd);
return EXIT_FAILURE;
}
close(fd);
printf("Module \"%s\" was successfully loaded\n", MOD_PATH);
return EXIT_SUCCESS;
}

What's the most efficient way to get source code of web page in C?

In PHP I can do it as simple as :
file_get_contents('http://stackoverflow.com/questions/ask');
What's the shortest code to do the same in C?
UPDATE
When I compile the sample with curl, got errors like this:
unresolved external symbol __imp__curl_easy_cleanup referenced in function _main
Use libcurl, refer to their example C snippets
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se");
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
Try the libcurl C interface
I should have commented the Richard Harrison good answer, but I have not 50 reputations points yet, so I put here as an answer my hint to ieplugin for compiling the code:
On Ubuntu 10.04 (and supposing you named the source file getpage.c):
sudo apt-get install libcurl4-dev
gcc getpage.c -lcurl -o getpage

Resources