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

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'

Related

#include errors detected for eigen library

I have been trying to use the Eigen library for c++ in VS code. I have seen various answers explaining how to solve the problem. I have opened the c_cpp_properties.json. I have my Eigen library :
But my MinGW shows different files from what I have seen in other answers and tutorials.
Error in code:
After this, what should I do? I tried adding the include path, but I cannot solve this.
Any help is highly appreciated.
You can move the whole Eigen Library to an included folder in IncludePath.
This is an example:
settings.json
"C_Cpp.default.includePath": [
"${workspaceFolder}/**",
"C:\\TDM-GCC-64\\include",
"C:\\TDM-GCC-64\\lib\\gcc\\x86_64-w64-mingw32\\10.3.0\\include",
"C:\\TDM-GCC-64\\lib\\gcc\\x86_64-w64-mingw32\\10.3.0\\include\\c++"
]
For you:
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:\\MinGW\\include",
"C:\\MinGW\\lib\\gcc\\x86_64-w64-mingw32\\{Your GCC Version}\\include",
"C:\\MinGW\\lib\\gcc\\x86_64-w64-mingw32\\{Your GCC Version}\\include\\c++"
]
}
]
You can put it in any of the folders like this.

Unable to include local C++ dependencies in VSCode

I am having an issue with writing code for my Robotic Operating System (ROS) project, however it's more related to dependency inclusion. Here is as far as I got with the code:
// ROS
#include <ros/ros.h>
// MoveIt
#include "moveit/moveit_ros/planning_interface/planning_scene_interface/include/*"
#include "moveit/moveit_ros/planning_scene_interface/planning_scene_interface.h"
#include <moveit/move_group_interface/move_group_interface.h>
// TF2
#include <tf2_geometry_msgs/tf2_geometry_msgs.h>
// The circle constant tau = 2*pi. One tau is one rotation in radians.
const double tau = 2 * M_PI;
int main(){
}
The issue is that dependency 2 through to 4 are not identified by VSCode. I attempted to include the necessary path within c_cpp_properties.json for dependency 2 as such:
"/home/george/ws_moveit/src/moveit/moveit_ros/planning_interface/planning_scene_interface/include/**"
The full c_cpp_properties.json looks as such:
{
"configurations": [
{
"browse": {
"databaseFilename": "${workspaceFolder}/.vscode/browse.vc.db",
"limitSymbolsToIncludedHeaders": false
},
"includePath": [
"/home/george/ws_moveit/devel/include/**",
"/opt/ros/noetic/include/**",
"/home/george/ws_moveit/src/moveit/moveit_planners/chomp/chomp_motion_planner/include/**",
"/home/george/ws_moveit/src/geometric_shapes/include/**",
"/home/george/ws_moveit/src/moveit/moveit_planners/chomp/chomp_interface/include/**",
"/home/george/ws_moveit/src/moveit_resources/prbt_ikfast_manipulator_plugin/include/**",
"/home/george/ws_moveit/src/moveit/moveit_ros/benchmarks/include/**",
"/home/george/ws_moveit/src/moveit/moveit_plugins/moveit_ros_control_interface/include/**",
"/home/george/ws_moveit/src/moveit/moveit_ros/move_group/include/**",
"/home/george/ws_moveit/src/moveit/moveit_ros/occupancy_map_monitor/include/**",
"/home/george/ws_moveit/src/moveit/moveit_ros/robot_interaction/include/**",
"/home/george/ws_moveit/src/moveit/moveit_ros/moveit_servo/include/**",
"/home/george/ws_moveit/src/moveit/moveit_setup_assistant/include/**",
"/home/george/ws_moveit/src/moveit/moveit_plugins/moveit_simple_controller_manager/include/**",
"/home/george/ws_moveit/src/moveit_visual_tools/include/**",
"/home/george/ws_moveit/src/pick_place/include/**",
"/home/george/ws_moveit/src/moveit/moveit_planners/pilz_industrial_motion_planner/include/**",
"/home/george/ws_moveit/src/moveit/moveit_planners/pilz_industrial_motion_planner_testutils/include/**",
"/home/george/ws_moveit/src/rviz_visual_tools/include/**",
"/home/george/ws_moveit/src/srdfdom/include/**",
"/usr/include/**",
"/opt/ros/noetic/include",
"/home/george/ws_moveit/src/moveit/moveit_ros/planning_interface/planning_scene_interface/include/**"
],
"name": "ROS",
"intelliSenseMode": "gcc-x64",
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu11",
"cppStandard": "c++14",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
This hasn't resolved my issue.
The way the packages are structured could be found here: https://github.com/ros-planning/moveit. It is essentially identical to the way I have them structured on my drive, with the the difference being the inclusion of the home/user directory.
The file in question should be located here:
https://github.com/ros-planning/moveit/tree/master/moveit_ros/planning_interface/planning_scene_interface/include/moveit/planning_scene_interface
Am I missing some detail or made an error?
#include "moveit/moveit_ros/planning_interface/planning_scene_interface/include/*"
Wildcards are not allowed in include statements, unless you have a very... weird preprocessor.
Reference the files you want to include directly, or use a master header file that directly contains all the files you want to include.

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

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! :)

Visual Studio Code - Include Path Problems Header Files C++ (MinGW)

I am pretty new to programm in C++ so please don't judge my problems with setting up my "Visual Studio Code" environment. I am trying to use the blaze math packages to solve quadratic programming problems. With the MinGW GCC I can compile the testfiles of blaze successfully via cmd and thus I want to use the GCC for VS Code.
OS: Windows 10.0.19041
GCC: gcc (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 7.3.0 || (cmd: gcc --version)
VS Code Extensions: C/C++ 0.27.1
Firstly I went through the VS Code tutorial for MinGW:
https://code.visualstudio.com/docs/cpp/config-mingw
This worked fine so I can easily compile my helloworld.cpp. The resulting tasks.json file looks like this
tasks.json.
As my package manager (for blaze or other packages) i use the vspkg-git:
https://learn.microsoft.com/en-us/cpp/build/vcpkg?view=msvc-160
Thus I am programming on Windows I can't use the "integrate" command to add the path to the includepath. So I have to do this manually.
My packages are in the folder with the absolute path
C:\Users\Johannes\Desktop\Masterthesis\vcpkg\vcpkg\packages
So I added the path on the "c_cpp_propertier.json"-file
{
"configurations": [
{
"name": "GCC",
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64",
"includePath": [
"${workspaceFolder}",
"C:/Users/Johannes/Desktop/Masterthesis/vcpkg/vcpkg/packages/**"
],
"compilerPath": "C:/Program Files/mingw-w64/x86_64-7.3.0-posix-seh-rt_v5-rev0/mingw64/bin/g++.exe",
"browse": {
"path": []
}
}
],
"version": 4
}
In the folder are several packages therefor I added the "/**" at the end of the path to enable the recursive search for header files.
My 'helloworld.cpp' file looks like this
#include <iostream>
#include <vector>
#include <string>
//#include <blaze/Math.h>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the
C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
My problem is that when I try to include a header-file from this path for example Math.h Visual Studio throws an Error
blaze/Math.h: No such file or directory
But when I right-click on the include and click "Go to Definition" VS Code opens the file. Optionbar and the opened file.
The Log-Diagnostics gives that information.
I guess someone that has experience with MinGW and additional packages in VS Code will solve that problem very simple, but I have read nearly every thread about those problems and didn't find anything matching mine.
Okay, i got the answere. The include path of the "c_cpp_properties.json" file is only for IntelliSense. This means that Visual Studio Code will find this packages and IntelliSense will suggest you the available headers from the pathes. This does not mean that the compiler can find these pathes. Therefor you have to add the pathes to the "tasks.json" file. As you can see above in the photo of the "tasks.json" file, there is a field called "args" which means "arguments". Those are the compiler arguments. You have to add the Path there too in Format "-I","C:/PathYouWishToAdd". This works fine!!

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