Compiling C source code for Windows using libcurl - c

Compiling C source code for Windows using libcurl using mingw32 and libcurl32. The command line below:
gcc -c -I "c:\curl\include" -L "c:\curl\lib" -o simple.exe simple.c
The compilation runs successfully, generating the exe file. But when I try to run it I get the message:
Unsupported 16-bit application. The program or feature "simple.exe" cannot start or run due to an incompatibility with 64-bit versions of Windows.
gcc (MinGW-W64 i686-ucrt-posix-dwarf, built by Brecht Sanders) 12.2.0
curl 7.83.1 (Windows) libcurl/7.83.1 Schannel Release-Date: 2022-05-13 Protocols: dict file ftp ftps http https imap imaps pop3 pop3s smtp smtps telnet tftp Features: AsynchDNS HSTS IPv6 Kerberos Largefile NTLM SPNEGO SSL SSPI UnixSockets
My code:
#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;
}

Unsupported 16-bit application. The program or feature "simple.exe" cannot start or run due to an incompatibility with 64-bit versions of Windows.
gcc -c compiles an object file, -o simple.exe usage is wrong, it should be -o simple.obj or -o simple.o. You get an object file named simple.exe, object files can't run.
Remove -c and add -lcurl to link the executable with libcurl:
gcc -I "c:\curl\include" -L "c:\curl\lib" -o simple.exe simple.c -lcurl

Related

Developing Paho-Mqtt-C Application on Host for Target having libraries

Basically, I have the paho-mqtt-c library installed in my rootfs using Yocto build environment. Since it was already included in meta-oe layer, I simply had to add that recipe to IMAGE_INSTALL_append variable.
I confirmed this by checking the following:
root#am65xx-evm:/usr/lib# ls | grep mqtt
libpaho-mqtt3a.so.1
libpaho-mqtt3a.so.1.0
libpaho-mqtt3as.so.1
libpaho-mqtt3as.so.1.0
libpaho-mqtt3c.so.1
libpaho-mqtt3c.so.1.0
libpaho-mqtt3cs.so.1
libpaho-mqtt3cs.so.1.0
Being a novice with building cross-compiled applications and setting up tool chains, I have a basic question which I couldn't exactly find an answer for.
Now that my target machine has the library installed, how to I develop an application on my host machine running Ubuntu 18.04 LTS?
I can do apt-get install and get the same library, but using the cross compiler to compile the C file, it is not able to see the the MQTT Library.
For example:
~/gcc-linaro-7.2.1-2017.11-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc mqtt-test.c -l paho-mqtt3c
mqtt-test.c:4:10: fatal error: MQTTClient.h: No such file or directory
#include "MQTTClient.h"
^~~~~~~~~~~~~~
compilation terminated.
LICENSE = "GPLv2"
LIC_FILES_CHKSUM = "file://COPYING;md5=b234ee4d69f5fce4486a80fdaf4a4263"
SRC_URI = "file://mqtt-test.c \
file://COPYING"
S = "${WORKDIR}"
TARGET_CC_ARCH += "${LDFLAGS}"
DEPENDS = "paho-mqtt-c"
do_compile() {
${CC} mqtt-test.c -o mqtt-test ${CFLAGS} -lpaho-mqtt3c
}
do_install() {
install -d ${D}${bindir}
install -m 0755 ${WORKDIR}/mqtt-test ${D}${bindir}
}
This is the recipe I used with the following directory structure:
Ignore the lmbench and hello-world - they were a sample from TI's tutorial.
Notice the DEPENDS = "paho-mqtt-c"
Apparently the -lpaho-mqtt3c flag with the do_compile() seemed to work this time.
I still wonder why I could't simply invoke the Linaro compiler and compile this separately.

compiling keepalived from source not working

i have been trying to compile and install keepalived like in the following link http://www.keepalived.org/doc/installing_keepalived.html
i'm building it on ubuntu/trusty64 vagrant machine
i have installed all the requirement specified in the document
sudo apt-get install curl gcc libssl-dev libnl-3-dev libnl-genl-3-dev libsnmp-dev
when i made 'make' command
it showed me the following error
gcc -g -O2 -I/usr/src/linux/include -I/usr/src/linux/include -
DLIBIPVS_USE_NL -Wall -Wunused -c -o libipvs.o libipvs.c
In file included from libipvs.h:13:0,
from libipvs.c:23:
ip_vs.h:15:29: fatal error: netlink/netlink.h: No such file or directory
#include <netlink/netlink.h> <-(the terminal saying that the error)
i tried that answer
libnl-3 includes broken?
i entered
sudo make -I /usr/include/libnl3
or
make -I /usr/include/libnl3
but both failed i'm kind new c compiling and building in linux so i would aperciate any help

I can not run C-OpenWire client for ActiveMQ after compilation

I'm trying to develop client in C for ActiveMQ using OpenWire and after compiling example main for OpenWire ActiveMQ on Ubuntu, when I try to run it I get this error:
bash: ./test: cannot execute binary file: Exec format error
I've tried it on both ubuntu 32 and 46 bit but it didn't work
Any Ideas?
Is there any other C alternative then OpenWire?
The problem is the -c flag, as it tells gcc to generate an object file and not an executable file.
Remove the -c flag:
gcc main.c -o test

'readline/readline.h' file not found

I have included:
#include "stdio.h"
#include <readline/readline.h>
#include <readline/history.h>
and my compiler includes the flag
-lreadline
but I am still receiving the error message:
fatal error: 'readline/readline.h' file not found
I am trying to use the function, readline();
Defined in more detail here: http://linux.die.net/man/3/readline
You reference a Linux distribution, so you need to install the readline development libraries
On Debian based platforms, like Ubuntu, you can run:
sudo apt-get install libreadline-dev
and that should install the correct headers in the correct places,.
If you use a platform with yum, like SUSE, then the command should be:
yum install readline-devel
This command helped me on linux mint when i had exact same problem
gcc filename.c -L/usr/include -lreadline -o filename
You could use alias if you compile it many times
Forexample:
alias compilefilename='gcc filename.c -L/usr/include -lreadline -o filename'

Cross-compiling for ARM while linking to libssh - libssh.so: file not recognized

I want to know if it is possible to link my application to libssh while cross-compiling with Sourcery toolchain for ARM. My host system is Ubuntu x86_64
:~/c/ssh$ arm-none-linux-gnueabi-gcc ssh.c -o arm `pkg-config --cflags --libs libssh`
cc1: warning: include location "/usr/local/include" is unsafe for cross-compilation [-Wpoison-system-directories]
/home/user/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_GNU_Linux/bin/../lib/gcc/arm-none-linux-gnueabi/4.6.1/../../../../arm-none-linux-gnueabi/bin/ld: warning: library search path "/usr/local/lib" is unsafe for cross-compilation
/usr/local/lib/libssh.so: file not recognized: File format not recognized
collect2: ld returned 1 exit status
My application compile fine with gcc using this command:
gcc ssh.c -o ssh -lssh
Adding the same -lssh flag while cross-compiling result in the following error:
:~/c/ssh$ arm-none-linux-gnueabi-gcc ssh.c -o arm -lssh
ssh.c:2:49: fatal error: libssh/libssh.h: No such file or directory
compilation terminated.
Your first attempt is trying to link the libssh.o from your host environment instead of your target environment. This is because the command "pkg-config --cflags --libs libssh" returns the package configuration of libssh on your host machine.
You will need to obtain or compile up a copy of libssh specifically for the target environment (ARM).
If compiling it yourself (likely your only option, for me at least a quick google did not reveal any suitable pre-built package) then you should specify a separate installation directory, eg. in your home directory somewhere. This will result in separate include and lib dirs, containing the cross compiled libssh, which you can reference from your own compilation commands, eg:
arm-none-linux-gnueabi-gcc -I{includedir} -L{libdir} ssh.c -o arm -lssh
Note that libssh in turn relies on other libraries - openssl and zlib - which you also have to cross-compile.

Resources