C++ program does not run in Code::Blocks - c++

While doing programming in Code::Blocks it compiles well for C but not for C++. Even for a "Hello World" program:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
it gives these errors:
-------------- Build: Debug in project ---------------
Compiling: main.cpp
Linking console executable: bin\Debug\project.exe
C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_throw.o):eh_throw.cc:(.text+0x7b): undefined reference to `__w32_sharedptr_unexpected'
C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_throw.o):eh_throw.cc:(.text+0x8c): undefined reference to `__w32_sharedptr_terminate'
C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x4e): undefined reference to `__w32_sharedptr'
C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0xb9): undefined reference to `__w32_sharedptr'
C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x179): undefined reference to `__w32_sharedptr'
C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x186): undefined reference to `__w32_sharedptr'
C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x1e3): undefined reference to `__w32_sharedptr'
C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x1ef): more undefined references to `__w32_sharedptr' follow
C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_terminate.o):eh_terminate.cc:(.text+0x67): undefined reference to `__w32_sharedptr_terminate'
C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_terminate.o):eh_terminate.cc:(.text+0x97): undefined reference to `__w32_sharedptr_unexpected'
C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_terminate.o):eh_terminate.cc:(.text+0xb3): undefined reference to `__w32_sharedptr_terminate'
C:\Program Files (x86)\CodeBlocks\MinGW\lib/libstdc++.a(eh_terminate.o):eh_terminate.cc:(.text+0xd3): undefined reference to `__w32_sharedptr_unexpected'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 1 seconds)
12 errors, 0 warnings

The errors you're getting indicate that the linker is having trouble locating __w32_sharedptr which is probably a dependency libstdc++ needs to work.
Normally the standard library and any dependencies it needs are linked in automatically when you build your project. However, as trojanfoe's comment indicates this is only true if you're compiling with g++. If you're building C++ code with gcc, the C++ standard library won't get included automatically since the gcc driver thinks it's compiling C code.
To verify what's actually happening in your codeblocks setup go to Settings->Compiler and Debugger->Global compiler settings(on the left)->under Toolchain executables tab. You should see something similar to this:
If your setup looks right but still refuses to build properly, enable full compiler logging and see what commands are actually being invoked by the IDE. You can find this under Global compiler settings->Other settings tab-> Compiler Log = Full command line. Note you might have to scroll a bit to the right to find the tab.
With full logging enabled, rebuild your project again and update your question with the commands used.
This is approximately what you should see in the log window when you rebuilt with the above options turned on:

Related

I am able to include hidsdi.h, and have the files, so how do I fix error "undefined reference to `HidD_GetAttributes(void*, _HIDD_ATTRIBUTES*)'"?

I have the following block of code:
#include <hidsdi.h>
HIDD_ATTRIBUTES DevAttributes;
LPGUID HidGuid;
HidD_GetHidGuid(HidGuid);
HidD_GetAttributes(hSerialPort, &DevAttributes);
cout << DevAttributes.VendorID << DevAttributes.ProductID << endl;
I am using visual studio code, and do not get any include error when I include the hidsdi.h file. Furthermore, I can hover the HidD_* functions and see the parameters / jump to their definition in msys64/mingw64/include/hidsdi.h
I verified the file is located at the path just specified, and my path environment variable includes "C:\msys64\mingw64\bin".
Therefore, I am lost as to why I am getting the following error message when I try to compile:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\user\AppData\Local\Temp\ccmQ19Ky.o:comportwin.cpp:(.text+0x42): undefined reference to `HidD_GetHidGuid(_GUID*)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\user\AppData\Local\Temp\ccmQ19Ky.o:comportwin.cpp:(.text+0x55): undefined reference to `HidD_GetAttributes(void*, _HIDD_ATTRIBUTES*)'
collect2.exe: error: ld returned 1 exit status
I tried using a PHIDD_ATTRIBUTES instance instead of HIDD_ATTRIBUTES as well, with no luck.
I know these sort of undefined reference questions are asked to death on here, but I can't for the life of my determine why I am getting this error when I see the .h file exists.
Any help would be much appreciated.
It is a linking error, not an "include" one.
You just need to add the library that has the symbols of "hidsid" to the linking step.
According with the documentation: https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/hidsdi/nf-hidsdi-hidd_getinputreport
It is Hid.lib

Can I install a library outside the library folder in Arduino?

I am working on an Arduino project with a friend and we need 2 external libraries, OneWire and DallasTemperature. I can easily install them on my computer and it will work completely fine, but I actually want to install them in my git folder, so anyone can instantly use my code. My folder structure is as follows:
project_folder/
> libraries/
> OneWire/
> OneWire.h and OneWire.cpp and so
> DallasTemperature/
> DallasTemperature.h and DallasTemperature.cpp and so
> project.ino
In the header files I had to change some references, mostly file paths, so #include <OneWire.h> became #include "../OneWire/OneWire.h" and so and and I got no errors there. In my project.ino I loaded both libraries using #include "libraries/OneWire/OneWire.h" and #include "libraries/DallasTemperature/DallasTemperature.h", which all seem to work, but when I verify my code I get the following error:
C:\Users\<my username>\AppData\Local\Temp\ccZFsuFb.ltrans0.ltrans.o: In function `global constructors keyed to 65535_0_project.ino.cpp.o.1999':
<artificial>:(.text.startup+0x5e): undefined reference to `OneWire::begin(unsigned char)'
<artificial>:(.text.startup+0x6a): undefined reference to `DallasTemperature::DallasTemperature(OneWire*)'
C:\Users\<my username>\AppData\Local\Temp\ccZFsuFb.ltrans0.ltrans.o: In function `setup':
C:\Users\<my username>\Documents\<project folder>/project.ino:13: undefined reference to `DallasTemperature::begin()'
C:\Users\<my username>\AppData\Local\Temp\ccZFsuFb.ltrans0.ltrans.o: In function `loop':
C:\Users\<my username>\Documents\<project folder>/project.ino:17: undefined reference to `DallasTemperature::requestTemperatures()'
C:\Users\<my username>\Documents\<project folder>/project.ino:19: undefined reference to `DallasTemperature::getTempCByIndex(unsigned char)'
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino Uno.
Is there a possibility to install these libraries in my project folder or do I really have to install them seperately on each system?
Is there a possibility to install these libraries in my project folder or do I really have to install them seperately on each system?
The answer is of course yes, First you need an IDE can make relative including
you can update your Arduino IDE for version >= 1.6.10 and restructure your folder to be as following
|project_folder/
|>src/
| |> OneWire/
| | > OneWire.h and OneWire.cpp and so
| |> DallasTemperature/
| | > DallasTemperature.h and DallasTemperature.cpp and so
|> project.ino
and then include them from your sketch, like this
#include "src/OneWire/OneWire.h"
#include "src/DallasTemperature/DallasTemperature.h"
Recommend option
use any other IDEs rather than Arduino IDE ... personally I prefer Atmel studio you can download atmel studio for free from the official site
and this lesson show how to convert Arduino sketches to work with atmel studio

Undefined reference to `WinMain#16' (Visual Studio Code)

I am new to visual studio code & I am trying to configure it with C++.
I have installed MinGW compiler and It is successfully installed (I tested it with a command g++ --version on a command prompt). I have also added the environment path in the system settings.
I have also installed Code runner extension in VS code, but when I try to compile my code, I get this error:
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../libmingw32.a(main.o):(.text.startup+0xb0): undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
I searched about this problem for a while but I am still unable to fix this issue. I have learned while searching that I am getting this error because I am not using the main function but I am just trying to compile simple hello world code.
#include<iostream>
using namespace std;
int main()
{
cout<<"Hello world";
}

MinGW in Code::Blocks not linking static OpenSSL Library

I'm trying to take advantage of some of OpenSSL's EC cryptography and ECDSA functionality to make a rudimentary blockchain simulation, but I've run into a major roadblock. I'm using the Code::Blocks IDE, and I've installed OpenSSL using the 64-bit binary found here: https://slproweb.com/products/Win32OpenSSL.html.
In Linker Settings, I added OpenSSL-Win64\lib\openssl.lib
In Search Directories > Compiler, I added OpenSSL-Win64\include
In Search Directories > Linker, I added OpenSSL-Win64\lib
However, when I compile, I get the "undefined reference to (function name)" error for every OpenSSL function I try to use. However, the compiler seems to be successfully including the header files, since it recognizes the new data types like EC_KEY. I suspect a linking error, but I'm not sure what could be wrong, since I have the link paths and static library linked as above.
Here is my build log:
mingw32-g++.exe -LD:\OpenSSL-Win64\lib -LD:\OpenSSL-Win64\include -LD:\OpenSSL-Win64 -o bin\Debug\BlockchainSim.exe obj\Debug\main.o obj\Debug\src\Block.o obj\Debug\src\BlockchainNode.o obj\Debug\src\Transaction.o obj\Debug\src\Valuable.o D:\OpenSSL-Win64\lib\openssl.lib
obj\Debug\src\BlockchainNode.o: In function `ZN14BlockchainNodeC2Ei':
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:18: undefined reference to `EC_KEY_new_by_curve_name'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:21: undefined reference to `BN_new'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:22: undefined reference to `EC_KEY_set_private_key'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:25: undefined reference to `BN_CTX_new'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:26: undefined reference to `BN_CTX_start'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:28: undefined reference to `EC_KEY_get0_group'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:29: undefined reference to `EC_POINT_new'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:30: undefined reference to `EC_POINT_mul'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:31: undefined reference to `EC_KEY_set_public_key'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:34: undefined reference to `EC_POINT_free'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:35: undefined reference to `BN_CTX_end'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:36: undefined reference to `BN_CTX_free'
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:37: undefined reference to `BN_clear_free'
obj\Debug\src\BlockchainNode.o: In function `ZN14BlockchainNodeD2Ev':
F:/School/Barrett/BlockchainSim/src/BlockchainNode.cpp:42: undefined reference to `EC_KEY_free'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
15 error(s), 0 warning(s) (0 minute(s), 0 second(s))
I'm hoping I'm just missing something simple here since I'm new to linking static libraries. A lot of similar issues seem to be solved by adding options to compile commands, but since I'm using Code::Blocks and linking OpenSSL as a static library, I'm not sure if those apply here. Any help is greatly appreciated.
Figured it out. The issue was that I had no C:/MinGW folder (as Code::Blocks installed MinGW within its own directory). I'm guessing the binary I used tries to detect existing compilers and compiles the library differently depending on what it finds.
I fixed it by installing default MinGW (with MSYS, in case that matters) with the default path (C:/MinGW) and then reinstalling the 32-bit binary OpenSSL linked in the original question, also in the default path (C:/OpenSSL-Win32). This made it so that within the C:/OpenSSL-Win32/lib folder, there was a new MinGW folder which I then included in the linker search directories.

How to Install/Use libcurl with C++ on Windows/Eclipse CDT

Can someone please explain how to use libcurl with C++ on Windows with Eclipse CDT/Code::Blocks or a similar IDE?
I'm very new to C++ but I know my way around Java very well.
I'm using MinGW but I keep getting this error:
C:\Core\src>g++ -I"C:\curl\include\curl" -L"C:\curl\lib64" -lcurldll core.cpp -o
core.exe
C:\Users\Bob\AppData\Local\Temp\cc2BV0HI.o:core.cpp:(.text+0xc81): undefined
reference to `_imp__curl_easy_init'
C:\Users\Bob\AppData\Local\Temp\cc2BV0HI.o:core.cpp:(.text+0xca7): undefined
reference to `_imp__curl_easy_setopt'
C:\Users\Bob\AppData\Local\Temp\cc2BV0HI.o:core.cpp:(.text+0xcc4): undefined
reference to `_imp__curl_easy_setopt'
C:\Users\Bob\AppData\Local\Temp\cc2BV0HI.o:core.cpp:(.text+0xcd1): undefined
reference to `_imp__curl_easy_perform'
C:\Users\Bob\AppData\Local\Temp\cc2BV0HI.o:core.cpp:(.text+0xce1): undefined
reference to `_imp__curl_easy_cleanup
What I've Tried:
If I give the wrong library path/name it will tell me that it can not find the library. So clearly it FOUND the libcurldll.a/libcurl.a files but it isn't linking with them properly.
I've tried putting the actual libcurl.dll file from the bin into every source folder possible in my project.
I've tried going to C/C++ General > Paths and Symbols then added "curl" and "curldll" to libraries and "C:\curl\lib64" to the library search path.
I've tried manually adding the -lcurl, -lcurldll, -DCURL_STATICLIB, -L"C:\curl\lib64" options to the MinGW Linker tool.
This has been stumping me for days. Please help.