I wanted to create a simple local web server using this library.
So, I created a new project in Visual Studio, created main.cpp and type in this code into my main.cpp.
#include "httplib.h"
using namespace httplib;
int main(void) {
Server svr;
svr.Get("/hi", [](const Request & /*req*/, Response &res) {
res.set_content("Hello World!", "text/plain");
});
svr.listen("localhost", 8080);
}
And I also created httplib.h and copied this code into my httplib.h.
I ran it. A blank terminal pop-up, then I go to localhost:8080/hi via Google Chrome and it works. There's "Hello World" output in there.
Then, I do the same thing with Visual Studio Code. But when I compile it, there's an error says
/usr/bin/ld: /tmp/ccUgoSsW.o: in function
'std::thread::thread<httplib::ThreadPool::worker, ,
void>(httplib::ThreadPool::worker&&)':
/usr/include/c++/9/thread:126:
undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
I searched a bit and found a solution. So, I compile it manually using Visual Studio Code terminal by typing "g++ main.cpp -o demo -pthread" and it compiled successfully. But, then I try to run the demo via terminal "./demo" it just doesn't do anything. I try to connect to localhost:8080/hi via Google Chrome but it said This site can’t be reached.
How to reproduce my problem
1- Create a folder
2- Create a main.cpp in that folder and type in this code into it
#include "httplib.h"
using namespace httplib;
int main(void) {
Server svr;
svr.Get("/hi", [](const Request & /*req*/, Response &res) {
res.set_content("Hello World!", "text/plain");
});
svr.listen("localhost", 8080);
}
3- Create a httplib.h file into the folder and copy this code into it
4- Compile it via Visual Studio Code terminal by using this command "g++ main.cpp -o demo -pthread"
5- Run the build via terminal by using this command "./demo"
6- Go to Google Chrome and type this address "localhost:8080/hi" If it can't be accessed then that's my problem
ps: I'm using WSL2 Ubuntu 20.04
Related
So I am having trouble building my .exe files in visual studio code for my current windows OS. for some reason, my .exe files, when I run a diagnostic on them, seem to only be compatible for windows 8, not 10.
Using
Processor architecture: AMDx64
system: x64 based PC
VS code version: 1.74.3
When creating a simple "Hello World" application I tried following this tutorial, and it didn't have any problems. It was when I followed the tutorial for importing external libraries that the problems ocured.
I’ve tried importing an external library, and used msys2 to install the files in the bin/include/lib folders for mingw64.
I set my include path to the include folder, and I’ve set my compiler to default. My JSON tasks document appears correct, and when I build the .exe file, it builds successfully… but it only builds an executable file compatible for windows eight
I ran the properties compatibility test, and this is the output I get
What exactly do I need to do in order to change the OS version to make it compatible to run on both the visual studio code terminal, and my system terminal as well?
the following is the output once I ran the build process
Starting build...
C:\msys64\mingw64\bin\cpp.exe -IC:\msys64\mingw64\include -fdiagnostics-color=always -g "D:\Documents\C++\VS_Code\FMT Import\FMTImport.cpp" -o "D:\Documents\C++\VS_Code\FMT Import\FMTImport.exe" -lfmt
>Build finished successfully.
edit:
config name:C:/msys64/mingw64/bin/g++.exe
compiler path: C:/msys64/mingw64/bin/g++.exe
intellisense mode: ${default}
edit 2:
running terminal attempts to build..
for g++
D:\Documents\C++\VS_Code\FMT Import>g++ -o FMTImport FMTImport.cpp
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\William\AppData\Local\Temp\ccshGhdE.o:FMTImport.cpp:(.text+0x8c): undefined reference to `fmt::v9::vprint(fmt::v9::basic_string_view<char>, fmt::v9::basic_format_args<fmt::v9::basic_format_context<fmt::v9::appender, char> >)'
collect2.exe: error: ld returned 1 exit status
for clang++
D:\Documents\C++\VS_Code\FMT Import>clang++ FMTImport.cpp -o FMTImport
FMTImport.cpp:1:10: fatal error: 'fmt/format.h' file not found
#include <fmt/format.h>
I've tried using Notepad++ to code c++ and followed a few tutorials on youtube, here's what I did:
-Installed gcc/g++ compiler using mingw64
-Installed NppExec plugin on N++
-Typed in the following compilier script and saved as C++:
NPP_SAVE cd $(CURRENT_DIRECTORY) g++ $(FILE_NAME) cmd /c $(CURRENT_DIRECTORY)\program.exe
Anyways whenever compiling a program, for example a simple program
#include <iostream>
using namespace std;
int main(){
cout << "Online\n";
system("pause"); //So that cmd doesn't disappear immeadiately on running.
return 0;
}
The console displays the following warning:
"C:\Users\pc\Desktop\Courses\Projects\C\program.exe' is not recognized as an internal or external command, operable program or batch file."
My question is, When I run the program on cmd, it runs perfectly but the error displayed during linking says that the folder does not exist in %PATH%
Any explanation?
Thank you!
Ok so, what I basically did was change the script,
cmd /c $(CURRENT_DIRECTORY)\program.exe
To be later
cmd /c $(CURRENT_DIRECTORY)\a.exe
the console worked fine and even received input
Here is a link to a similar problem:
How to compile/execute C++ code from within Notepad++
Hi guys i installed the vs code and downloaded the needed extensions (C/C++-code runner)
and i installed Mingw
but when i run this test code
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
it shows this
PS C:\Users\faroo\Desktop\C++ 101> cd "c:\Users\faroo\Desktop\C++ 101\" ; if ($?) { g++ calssCar.cpp -o calssCar } ; if ($?) { .\calssCar }
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): in function `main':
C:/_/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
can you please help me
The compiler is working fine but the linker trying to link it as a Windows GUI App, which uses WinMain() instead of main() as entry point of application.
You should recheck your compiler flags in vsCode settings. If I remember properly, for Mingw-w64, the flag is -mwindows.
I suspect you probably used some template to create vscode project but chose the wrong one (gui app instead of console app)
Also, as a beginner I would recommend using TDM-GCC instead of msys2, as it's better intergreted with Windows and is easier to work with.
one thing i noticed is IF YOU RUN YOUR CODE WITHOUT SAVING it is giving this error.
try RUN after saving
I suggest you create a new cpp file with another title.Then copy your code to the new file.Try to run the code in the new file again.Maybe it can works.
Such error usually happen to my laptop.It dosen't matter.
It's because maybe you haven't set your path of Mingw. Let me guide you to do that if you're using Windows:
Install Mingw (which you already have), like I have installed it in C:\Mingw
Then open Mingw\bin folder from that drive, like I have opened C:\MinGW\bin
Copy "C:\MinGW\bin" and then open This Pc.
Right-click on open space and click properties.
Go to Advanced system settings.
Click on the Environment variable.
Then in system variables, double click on PATH.
Click on new, and paste your path there.
Press ok and now run your code again in Vscode, it'll surely work!
i wrote a program in my mac using sublime text as the plateform..
#include<iostream>
using namespace std;
int main()
{
cout<<"HELLOW WORLD";
return 0;
}
this was my program..
i saved it in desktop as hellow.cpp
while compiling on mac terminal as g++ hellow.cpp, i found an error
adarshs-MacBook-Air:Desktop adarshak$ g++ hellow.cpp
xcrun: error: invalid active developer path
(/Library/Developer/CommandLineTools), missing xcrun at:
/Library/Developer/CommandLineTools/usr/bin/xcrun
anyone plese help me to find out the error
It sounds like you don't have the command line developer tools installed. Run this command from Terminal once:
xcode-select --install
This will bring up the download & installation UI. Follow this through to the end. (It may take a while depending on the speed of your internet connection.)
From then on compiling should work.
i downloaded gsoap 2.8.14, configure and install with the following commands:
./configure --disable-ssl --enable-samples --enable-debug
make
make install
im tried to compile gsoap sample "hello". so i took the the wsdl file from the sample and did the following:
wsdl2h -s -o hello.h h.wsdl
soapcpp2 hello.h
i copied the generated files into a new eclipse c++ project and excluded soapClientLib.cpp and soapServerLib.cpp because i was receiving errors like
multiple definition of .....
i then created a helloserver.cpp and here is the content:
#include "soapH.h"
#include "Service.nsmap"
int main()
{
return soap_serve(soap_new);
}
int __ns1__hello(struct soap *soap, char* helloReq, char* &helloResponse)
{
return SOAP_OK;
}
when i build in eclipse, i receives an error:
...soapServer.cpp:77 undefined reference to __ns1__hello(soap*,_ns2_hello*, _ns__helloResponse*)
when i trace to soapServer.cpp, this line is getting the error:
soap->error=__ns1_hello(soap,soap_tmp___ns1_hello.ns2__hello,&ns2__helloResponse);
why am i getting this error? im using the sample hello wsdl from gsoap
Well as you can see from the error message (and the soapServer.cpp code) you are supposed to write a function
int __ns1__hello(struct soap *soap,
_ns2_hello* helloReq,
_ns__helloResponse* helloResponse)
{
return SOAP_OK;
}
not the function you wrote.