I can't seem to link g++ to my installed libcurl - c++

I am new to programming in c++. I am trying to compile a code using curl in it. My visual studio code is able to find the file (as it autocompletes as well), but when I run "build task g++" it says the following:
C:\Users\matth\AppData\Local\Temp\cceVTTmY.o:test.cpp:(.text+0xf): undefined reference to `_imp__curl_easy_init'
C:\Users\matth\AppData\Local\Temp\cceVTTmY.o:test.cpp:(.text+0x21): undefined reference to `_imp__curl_easy_cleanup'
collect2.exe: error: ld returned 1 exit status
I don't get it. I installed curl with vcpkge and integrated it, so visual studio code can actually find it.
I used this in my args for tasks.json for g++:
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"C:\\Program Files\\vcpkg-master\\installed\\x86-windows\\include",
"-L",
"C:\\Program Files\\vcpkg-master\\installed\\x86-windows\\lib",
"-static"
],
I have also tried adding '-lcurl' but then the cmd actually responds saying 'cant't find lcurl'.
I am struggling for a lot of hours now (as I said, new to c++) and I am getting really frustrated.
This is the code I am trying to compile:
#define CURL_STATICLIB
#include <curl/curl.h>
int main()
{
CURL *curl;
curl = curl_easy_init();
curl_easy_cleanup(curl);
return 0;
}
Anyone that knows how to fix this problem? All solutions I could find didn't work, so I decided to make my own account and try it this way...
Thanks a lot in advance, if you need more info, tell me! :)

Related

Using clang-tidy to analize embedded GCC project

I'm trying to use clang-tidy to parse my project, compiled by arm-none-eabi-g++.
Unfortunately, clang-tidy is not able to find compiler headers, even when given the include path to them.
My compile_commands.json is
[
{
"directory": "C:/LMA/repo/clang-tidy",
"arguments": [
"arm-none-eabi-c++",
"-IC:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/c++/9.2.1",
"-IC:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/c++/9.2.1/arm-none-eabi/arm/v5te/hard",
"test.cpp"
],
"file": "./test.cpp" } ]
And the example test.cpp file is:
#include <cstdint>
#include <cstdlib>
int test()
{
int temp;
return 0;
}
Clang-tidy shows error:
C:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/c++/9.2.1\cstdlib:75:15: error: 'stdlib.h' file not found [clang-diagnostic-error]
#include_next <stdlib.h>
So, it properly finds and includes cstdlib, however it is not able to find stdint.h, which is located in the exactly same folder. What's even more irritaiting, it does not include stdlib.h, even when I add
-include C:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/c++/9.2.1/stdlib.h
to compiler arguments in order to force preinclude.
Any suggestions how to fix this issue are very much appreciated.
This is a bit old, but I was having a similar issue so I figured I'd report an answer here in case anyone else comes across this while searching. It appears clang doesn't know where to find the standard library headers when compiling using arm-none-eabi. I succeeded by simply adding them.
In your case you're still missing the C headers directory, so you need to add this:
C:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/
(assuming the directory structure is similar to mine).
So your compile_commands.json would be:
[ {
"directory": "C:/LMA/repo/clang-tidy",
"arguments": [
"arm-none-eabi-g++",
"-IC:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include",
"-IC:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/c++/9.2.1",
"-IC:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/c++/9.2.1/arm-none-eabi/arm/v5te/hard",
"test.cpp"
],
"file": "./test.cpp"
} ]
You should also have arm-none-eabi-g++ instead of arm-none-eabi-c++ I believe.

How to setup sublime 3 build for C++ with multiple files

First of all, I'm a total noob on SO, so please go easy on me :)
That being said, my question might be really easy to answer:
How would I use sublime text 3 for c++ with multiple files (header/implementation files)
I've been trying some build systems provided by other people,
this is the one I'm currently using and seems to work just fine for compiling single .cpp files but is telling me some errors which I don't understand
here is my build config using g++
{
"cmd": ["g++.exe", "-std=c++14", "-o", "$file_base_name", "$file", "&&", "start", "cmd", "/c", "$file_base_name & echo. & echo. & pause"],
"shell": true,
"selector": "source.c++"}
here is my main.cpp:
#include <iostream>
#include <string>
#include "num.h"
using namespace std;
int main(){
Num n(35);
cout << n.getNum() << endl;
return 0;}
here is my num.h file:
#ifndef NUM_H
#define NUM_H
class Num
{
private:
int num;
public:
Num(int n);
int getNum();
};
#endif
and here is my num.cpp:
#include "num.h"
#include <iostream>
#include <stdlib.h>
Num::Num(int x){
num = x;
}
int Num::getNum()
{
return num;
}
all files are in the same directory and whenever I'm using g++ on the command like like this: (g++ (tdm64-1) 5.1.0)
g++ main.cpp num.cpp
there is no problem and everything works just fine
but whenever I'm trying to build it with sublime text it throws me this error
C:\Users\GEBRUI~1\AppData\Local\Temp\ccqq94my.o:main.cpp:(.text+0x1a): undefined reference to `Num::Num(int)'
C:\Users\GEBRUI~1\AppData\Local\Temp\ccqq94my.o:main.cpp:(.text+0x26): undefined reference to `Num::getNum()'
collect2.exe: error: ld returned 1 exit status
[Finished in 0.8s]
I would really appreciate it if someone could tell me what I'm doing wrong here :)
Once you start getting into the realm of needing to compile multiple files, it's really best to start using make or CMake instead of trying to build the compile commands yourself on the command line or in a build system. This way you wouldn't have to edit your build system every time you added a new file to your project.
There is a Make build system that comes with Sublime, but you will need to generate the Makefile externally, either by hand or using tools like autoconf and automake. The CMakeBuilder package looks like it would be useful for working with CMake (I haven't used it myself), although you can of course use external tools as well.

Using Qt with Visual Studio Code (Windows)

The following are installed:
Visual Studio Code (1.45.1)
Visual Studio 2019 Community (in order to use the MSVC cl.exe compiler)
Qt 5.15.0 (installed to C:\Qt)
Visual Studio Code has been launched after running Visual Studio Command Prompt so that the environment is set correctly for cl.exe. The ms-vscode.cpptools extension has been installed in Visual Studio Code and includePath is set to:
"includePath": [
"${workspaceFolder}/**",
"${INCLUDE}",
"C:/Qt/5.15.0/msvc2019_64/include/**"
],
This file hw.cppcompiles and runs fine:
#include <iostream>
int main()
{
std::cout << "Hello world!";
return 0;
}
The command used in tasks.json is:
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
Alternatively, from the in-built Terminal inside Visual Studio Code, the command "cl /EHsc /MD /O2 hw.cpp /link /out:hw.exe" compiles everything correctly and hw.exe can be executed.
But when I attempt to use Qt as follows it fails to compile:
#include <QString>
int main()
{
QString test("Hello world!");
qDebug() << test;
return 0;
}
The compiler reports "fatal error C1083: Cannot open include file: 'QString': No such file or directory". IntelliSense does find QString.h, which opens when I press Ctrl and click QString (at the top).
What am I missing?
UPDATE
Thanks to comments from #rioV8, I've investigated /link options for cl.exe. The task arguments have been updated to:
"args": [
"/EHsc",
"/MD",
"/O2",
"/IC:\\Qt\\5.15.0\\msvc2019_64\\include",
"/IC:\\Qt\\5.15.0\\msvc2019_64\\include\\QtCore",
"${file}",
"/link",
"/LIBPATH:C:\\Qt\\5.15.0\\msvc2019_64\\lib",
"Qt5Core.lib",
"qtmain.lib",
"/OUT:${fileDirname}\\${fileBasenameNoExtension}.exe"
],
This has improved things slightly. hw.cpp now compiles and generates hw.obj, but now I get linker errors (one for each .lib):
warning LNK4272: library machine type 'x64' conflicts with target machine type 'x86'
This is followed by fatal error LNK1120: 2 unresolved externals.
Getting closer, but still not linking.
There has been a detailled guide on the KDAB blog recently.
Overview of VS Code for Qt developers:
https://www.kdab.com/using-visual-studio-code-for-writing-qt-applications/
Technical guide:
https://www.kdab.com/using-visual-studio-code-for-qt-apps-pt-1/
https://www.kdab.com/using-visual-studio-code-for-qt-apps-pt-2/
The steps in the technical guide look very similar to the issues you are describing ;-)

How do I transfer my C++ solution made in Visual Studio for Windows to Visual Studio for Mac?

I made a multiple solutions with C++ in Visual Studio at a tech camp on a Windows computer. Unfortunately, I do not have a windows computer at home, so I tried using Visual Studio for Mac 2019. This alert shows up whenever I try to run any of my solutions that says "This project type is not supported by Visual Studio Community 2019 for Mac. When I build the solution, it says it the build is successful, but the run with or without debugging options are grayed out. How can I fix this?
I was able to get it to work using a Windows 10 Parallels, and it said it needed to update the solution because I had made it in Visual Studio 2017. I updated it and it worked, but my program runs very slow. After doing this, it still does not work on Visual Studio for Mac.
Here is my main .cpp file named MyFirstProgram.cpp:
#include "pch.h"
#include "Main.h"
#include <string>
using namespace std;
int main()
{
cout << "My header file works!" << endl;
}
The pch.cpp file:
#include "pch.h"
The pch.h file:
#ifndef PCH_H
#define PCH_H
#endif //PCH_H
And the Main.h file:
#pragma once
#include <iostream>
#include <string>
To build your C++ project using Visual Studio code on Mac, ensure you have C/C++ build tools installed.
Go to your Marketplace on Visual studio, Click on view, select Extensions. Type C++ on the search, and select the extension by Microsoft and install
Create a new project
Copy and paste your codes in a new main.cpp and MyFirstProgram.cpp file.
Try rebuilding
Alternatively, you can use Xcode or Jetbrain CLion on Mac
Another trick to build your cpp program on Mac or Linux without using any heavy IDEs is building a simple task file
**Mac has a g++ compiler **
Create a folder yourDirectoryName
create a file main.cpp (you can copy and paste your codes in here)
create a file json file
** go to the top bar on visual studio, select Terminal > configure Default Build Task > select other**
it would create a hidden json file for you. modify the json file to run your main.cpp file
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"main.cpp",
"-o",
"${yourDirectoryName}"
],
},
{
"label": "run",
"type": "shell",
"command": "./${yourDirectoryName}",
"dependsOn": [
"build"
],
}
]
}
The task is called run.
to run it, go to terminal, select run task.
click on the task run > continue without scanning the task output

I cannot figure out the "undefined reference to Winmain#16 error"

Before I start, I know there are a lot of other questions on stack that are the same as mine. Trust me, I've checked them all, and I wouldn't be asking this question if they had helped me in any way shape or form. Also I would very much like an easy to understand answer, because while looking at the other questions, it took me 10 minutes of looking at them just to figure out what they meant. That being said, let's get to my problem.
I am trying to get SDL to work with Visual Studio Code (Not Visual Studio. My computer doesn't have enough space for Visual Studio or I'd be using it.) I am in the tasks.json file trying to link the SDL2.lib library to the file I'm working with, main.cpp (I'm using c++). I have moved all the files in the SDL include and lib folders to be loose in my mingw bin folder. This is my tasks.json file:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-o",
"-c",
"C:/MinGW/bin/main.cpp",
"C:/MinGW/bin/SDL2.lib",
"-lmingw32"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [
"$gcc"
]
},
]
}
And here is my main.cpp file:
#include "windows.h"
#include "SDL.h"
#include <iostream>
int main(int argc, char** argv) {
SDL_Init(SDL_INIT_EVERYTHING);
return 0;
}
I get the following error:
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1
From what I can tell, this error is fairly common, but I have been keeping track, and over the past two weeks, I have tried 37 different ways to solve this problem (including the solutions to the other posts about this). I don't know what the problem is, but maybe someone here can figure it out. I have posted this in four other forums with no responses and I am so lost. It's really hard to be enthusiastic right now, but please respond and thank you in advance!
Try adding this line before your SDL include:
#define SDL_MAIN_HANDLED
Look here for more details:
undefined reference to `WinMain#16'