Hello,
I am building a C++ program which needs to send information to an FTP server with the help of curl library. For the purpose I have created a docker image and a program with the help of ApolloX on VS Code for Apalis iMX6 board.
This is the code that I am using:
#include <iostream>
#include <curl/curl.h>
int ftpConnect(const char* server, const char* username, const char* password) {
CURL* curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, server);
curl_easy_setopt(curl, CURLOPT_USERPWD, (std::string(username) + ":" + std::string(password)).c_str());
curl_easy_setopt(curl, CURLOPT_UPLOAD, 0L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
std::cerr << "Failed to connect to FTP server: " << curl_easy_strerror(res) << std::endl;
curl_easy_cleanup(curl);
return 1;
}
curl_easy_cleanup(curl);
std::cout << "Connected to FTP server successfully." << std::endl;
return 0;
}
return 1;
}
int ftpUpload(const char* server, const char* username, const char* password, const char* filePath) {
CURL* curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, server);
curl_easy_setopt(curl, CURLOPT_USERPWD, (std::string(username) + ":" + std::string(password)).c_str());
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_READDATA, fopen(filePath, "rb"));
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
std::cerr << "Failed to upload file: " << curl_easy_strerror(res) << std::endl;
curl_easy_cleanup(curl);
return 1;
}
curl_easy_cleanup(curl);
std::cout << "File uploaded successfully." << std::endl;
return 0;
}
return 1;
}
int main() {
const char* server = "10.58.63.175";
const char* username = "Torizon";
const char* password = "69";
const char* filePath = "/media/card/2023-04-14.txt";
// Connect to FTP server
int connectResult = ftpConnect(server, username, password);
if (connectResult != 0) {
std::cerr << "Failed to connect to FTP server." << std::endl;
return 1;
}
// Upload file to FTP server
int uploadResult = ftpUpload(server, username, password, filePath);
if (uploadResult != 0) {
std::cerr << "Failed to upload file to FTP server." << std::endl;
return 1;
}
return 0;
}
The problem is I can’t link the library to the docker image apparently.
In the Dockerfile.sdk I have included libcurl4-gnutls-dev so it is installed in my docker container.
When I do just this, I get the following error:
app/src/internet_manager.cpp:38: undefined reference to `curl_easy_cleanup'
collect2: error: ld returned 1 exit status
make: *** [Makefile:47: armhf/debug/pesho] Error 1
But if I do not include this then the code crashed on the #include of the library. So that would tell me that after using this command I successfully install curl on the docker image.
From what I saw on the internet this is a problem for not linking curl so I tried to link it in the Makefile the following ways:
LDFLAGS := -lcurl
LDFLAGS := -L/usr/lib/arm-linux-gnueabihf/ -lcurl
LDFLAGS := -L/usr/lib/x86_64-linux-gnu/
LDFLAGS := -L/usr/bin/curl -lcurl
For the paths for curl in the docker image and in the WSL2 ubuntu system that I use. All of them get this error:
*
bug/main.o -o armhf/debug/pesho -L/usr/lib/arm-linux-gnueabihf/ -lcurl
/usr/lib/gcc-cross/arm-linux-gnueabihf/10/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lcurl
collect2: error: ld returned 1 exit status
make: *** [Makefile:47: armhf/debug/pesho] Error 1
The weird part is I have checked curl and it is installed both on the host machine and in the Debian based docker container that the program is running on.
What a colleague of mine try to do is run curl locally in the src file to see if it is accessible in there and he was not able to run it.
These are the docker images I have:
Where the first one, pesho-debeliq-debug is the used container.
My colleague has a suspicion that curl is not available on the debian-cross-toolchain-ssh-arm64 which should be the container to link the code.
And we are wondering if we should link the curl there somehow or add curl library to this container as well in order to work.
Or are we doing something wrong in these configurations to be missing something?