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

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";
}

Related

How do I interpret and resolve "dyld: Symbol not found ..." [duplicate]

After compiling, I am trying to run libuv sample program:
#include <stdio.h>
#include <uv.h>
int main() {
uv_loop_t *loop = uv_loop_new();
printf("Now quitting.\n");
uv_run(loop, UV_RUN_DEFAULT);
return 0;
}
But, when try to run, I get the following error:
**/tmp/ccHTpspB.o: In function `main':
main.c:(.text+0x9): undefined reference to `uv_loop_new'
main.c:(.text+0x28): undefined reference to `uv_run'
collect2: error: ld returned 1 exit status**
Where did I go wrong ?
PS: It doesn't work with #include "uv.h"
You need to link the libuv.a with your compiled code and the linker doesn't know where to find the compiled libuv.
To give you a better answer I would need to see you compile command but in the meantime I would strongly recommend this video where Ryan builds a sample libuv project.
The actual code he uses is a little out of date as the API has changed but I think you will find the start where he puts a project together very enlightening.
http://vimeo.com/24713213
In ubuntu I have used following command with success:
gcc sample.c -luv

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.

C++ - undefined reference to `__gxx_personality_seh0'

I am having an issue when trying to build my C++ application in Eclipse (neon.2). This application is simply supposed to create a file, print the numbers 15-210, save the file, then close. This is the error I am getting each time I try to build it:
17:59:42 **** Incremental Build of configuration Debug for project Project1 ****
Info: Internal Builder is used for build
g++ -o Project1.exe Q1.o Q2.o displayHeader.o
Q2.o:Q2.cpp:(.data+0x0): undefined reference to `__gxx_personality_seh0'
Q2.o:Q2.cpp:(.xdata+0x10): undefined reference to `__gxx_personality_seh0'
collect2.exe: error: ld returned 1 exit status
17:59:44 Build Finished (took 1s.391ms)
This only happens when I attempt to use either ofstream or ifstream in my program. I have successfully built & ran other (very simple) C++ applications that don't use this in Eclipse, but when I do, I get this error.
Here is my code:
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
int main(){
ofstream outfile;
outfile.open("nums.txt");
for (int i = 15; i < 211; i++) {
outfile << i << " ";
}
outfile.close();
return 0;
}
I created the project with MingW, and I have the /bin folder in my environment path variable, and under Project/Properties/C/C++ Build/Environment all of the variables are set. I've been searching through forums and tutorials like crazy to try and see if I missed something during installation of either Eclipse or MingW, but I don't see any issues.
I hope I provided adequate information but please let me know if anything else could help. I've been pulling my hair out over this for almost a week. And thank you so much for any help.
You didn't do anything wrong or miss anything during installation!
I had this issue before , this occurs in MingW compiler and ofstream objects, you should change this command
x86_64-w64-mingw32-gcc [your code file]
with this :
x86_64-w64-mingw32-g++ [your code file]
In short , change gcc with c++ in Eclipse or command line.
My opinion, use MS Visual Studio for compiling C++ code in Windows, its compiler give you more features and flexibility and you could develop faster.

Openssl Build Issue with Android NDK r8

I am trying to build Openssl inside my NDK app . I am constantly getting linking error even after following all necessary steps expected by Android ndk build. I am using ndk-build command with ndk supported library. i could see libcrypto.So bieng compiled and linked successfully
Compile thumb : crypto <= sha512-armv4.S
SharedLibrary : libcrypto.so
Install : libcrypto.so => /Users/<me>/Downloads/paddybyers-openssl-android-2b40b8b/libs/armeabi/libcrypto.so
but openssl which starts after this is failing for some reason .I tried on all possible ways including building on eclipse as well as on command line.I even tried to build the openssl seperately as a stand alone project. But it always stops at the same level.
My configuration :
Using mac OS x
NDK r8
Openssl source from : https://github.com/eighthave/openssl-android.git (I tried building on guardproject https://github.com/guardianproject/openssl-android.git )
it is throwing an error before it starts compiling openssl after creating libcrypto.So
****Compile thumb : ssl <= ssl_algs.c
Compile thumb : ssl <= bio_ssl.c
Compile thumb : ssl <= ssl_err.c
Compile thumb : ssl <= kssl.c
SharedLibrary : libssl.so
Executable : openssl
/Users/me/Documents/android/android-ndk/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6.x-google/../../../../arm-linux-androideabi/bin/ld: warning: libz.so, needed by ./obj/local/armeabi/libcrypto.so, not found (try using -rpath or -rpath-link)
./obj/local/armeabi/libcrypto.so: undefined reference to `zError'
./obj/local/armeabi/libcrypto.so: undefined reference to `inflateEnd'
./obj/local/armeabi/libcrypto.so: undefined reference to `deflate'
./obj/local/armeabi/libcrypto.so: undefined reference to `deflateInit_'
./obj/local/armeabi/libcrypto.so: undefined reference to `inflate'
./obj/local/armeabi/libcrypto.so: undefined reference to `deflateEnd'
./obj/local/armeabi/libcrypto.so: undefined reference to `inflateInit_'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/openssl] Error 1**** }
I would really appreciate if some one help me out on this ?
I ran into the same issue and fixed it by modifying OpenSSL1.0.1cForAndroid/crypto/Android.mk, adding libzib to the export list:
crypto/Android.mk
LOCAL_EXPORT_LDLIBS := -lz
Crypto/Android.mk does have "LOCAL_LDLIBS += -lz" and so it successfully links it when building libcrypto but apparently it's not enough to propagate it to when linking in ssl (?).
I'm using ndk r8b, gcc 4.6
Try adding the following line in your OpenSSL1.0.1cForAndroid\jni\Application.mk
NDK_TOOLCHAIN_VERSION=4.4.3
I just fixed the same problem by doing this.
Most likely you have a linking related issue, try to read the answer to this question
How to link any library in ndk application
Looks like you need to link against zlib as well (or disable zlib support in OpenSSL). You can do this with -lz (It's part of Android)
I was also facing same problem with android ndk r8e. Then I have downloaded openssl-static-android form git. That worked like charm. And the version of openssl you are using is successfully build in android ndk r7e. And one more thing, if you want to link this .so files to another library file or to make .apk, I will suggest you to use .a files instead of .so. Because .so files sometimes causes linker problem. If you are intended to use .a files you can use openssl-static-android from git. It works fine with all the versions of ndk.
N.B: If you are using android ndk r8e, I suggest you should check the binary files to check whether it is cleaned or not. Because there is a bug in android ndk r8e.

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

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: