c++ cgi on linux server - c++

I am pretty new to CGI's and wanted to give them a try. Here is my situation:
My website is hosted on a linux server, I have contacted the hosting company and they said as long as I use the cgi-bin folder there is no problem. So I wrote a very short c++ program that just output some text:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello from c++ cgi";
return 0;
}
Since its a linux server (and I am running Windows) I suppose I needed to compile the source in linux so a installed a virtual machine running Ubuntu. I then compiled the source (tested it in Ubuntu) and placed the binary file in the cgi-bin folder. Then I used a simple html anchor to link to it:
T
But when I click the link on my page I get the "500 Internal Server Error" message. I have also tried to rename the binary file to "cgiTest.cgi" and update the link but still the same problem.
Anybody have any idea where my problem lies?

You forgot to output any HTTP headers (the error log entry would give you a hint).
#include <iostream>
using namespace std;
int main()
{
cout << "Content-Type: text/plain" << endl << endl;
cout << "Hello from c++ cgi";
return 0;
}

Thanks for the help, turns out I had to change the permissions and the hosting server is 32bit while I compiled on 64bit Ubuntu. I now compiled on 32bit Ubuntu and voila!, its running. Thanks to all for the help in solving this issue!

Related

Necessary extensions for VS code with Windows OS

I am a first time C++ user, and I have been working for 8 hours trying to build and compile the simple "Hello World" program with C++ in Visual Studio Code. I have CygWin64, but I'm not sure if it's connected to my VSCode. I have installed the extensions C/C++, C/C++ Compile Run, C++ Intellisense, Clang-Format, and Easy C++ projects.
So far I have tried
#include <iostream.h>
main()
{
cout<< "Hi there";
return 0;
}
and
#include <iostream.h>
int main() {
std::cout << "Hello Easy C++ project!" << std::endl;
}
Using iostream.h helped me to get to work (it wouldn't at first), but I'm not sure if that is helpful, since other posts say that .h is very archaic. I have also tried editing my c_cpp_properties.json file. Sadly, I still get the message:
"> Executing task: bash -c "make run" <
'bash' is not recognized as an internal or external command,
operable program or batch file.
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it."
I am not sure if I need to install an alternative to Clang (I haven't found one), or run something initially on Cygwin64.
I have been looking online for suggestions and following pages such as https://dev.to/acharluk/developing-c-with-visual-studio-code-4pb9 and https://github.com/Microsoft/WSL/issues/1598, but I still can't seem to get around this problem.
Any help would be very appreciated.
Thanks,
Anne

How to integrate MySQL with codeblocks IDE

I have been searching solution for this problem whole day.Any help would make me grateful. I am using Code::blocks IDE 16.01. I want to connect from my IDE to my MySQL database server. I have tried a lot of options but yet to succeed. I have my MYSQL INSTALLER setup already and have downloaded the mysql connector and mysql server. I am gonna show you the code and the way i have already tried.
This is my simple code
#include <iostream>
#include <windows.h>
#include <mysql.h>
using namespace std;
int main()
{
MYSQL* conn;
conn = mysql_init(NULL);
if (mysql_real_connect(conn,"localhost","root","","test",0,NULL,0) !=0)
{
cout << "Succesfully Connected to MySQL database xxxx" << endl;
}
mysql_close(conn);
return 0;
}
I have my MySQL setup in C drive and I have linked that as below
After doing all the work I have been shown the following errors
Please someone help me. Thanks in advance. Please feel free to ask If you guyz need anything to know.
In third step of your CodeBlocks setup you must specify the lib path (and not the include path), as below:
C:\Program Files\MySQL\MySQL Server 8.0\lib .
Then rebuilds your application.

Query on ACE framework

Of late, I am facing a problem with a code snippet which doesn't return expected values and this is causing an application to fail.
The code is built on Redhat linux 7.1 using the following command -
g++ ace-test.cpp -I<path-to-ace-6.2-root> -L<path-to-ace-6.2-root>/ace/Linux -g -lACE
The code snippet being built is pasted below -
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "ace/MEM_Addr.h"
#include <iostream>
using namespace std;
int main(int argc, char* []) {
ACE_MEM_Addr addr ;
cout << "ACE_MEM_Addr::addr.get_host_name() " << addr.get_host_name() << endl ;
}
On execution, the code prints the following line
ACE_MEM_Addr::addr.get_host_name() **unknown**
on a system running linux which was recently patched.
On a different system which was not patched, the same code returns the correct hostname.
I am trying to figure out which patch caused this problem, but couldn't make much progress.
If someone can please extend a bit of help, would greatly appreciate it.
If required, I can share the list of patches which were applied.
Finally managed to find the culprit.
Here are the entries in /etc/nsswitch.conf:
hosts: files dns - (On the host without patches)
versus
hosts: files dns myhostname - (On the host which was patched)
Removal of the myhostname entry in the latter fixed the problem.
Associated RHEL knowledge base entry -
https://access.redhat.com/solutions/2766251
Thanks a bunch for all the help !

Variant on: New to Xcode can't open files in c++?

Like the questioner in "New to Xcode can't open files in c++?" I'm learning Xcode and OS X (I'm using Xcode 7 on a Yosemite mac).
I can get the code to work perfectly when I build and run it, but can't get the executable to work when I try to run it as a stand alone program.
I'm trying to translate some games I've written on a PC in C++ using SFML.
There has to be a way to save high scores and previous games within an app, but this has me stymied.
This is the sample code I used based on the previous question:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin;
ofstream fout;
string input;
fin.open("inputFile.txt");
if(fin.fail())
cout << "File failed to open." << endl;
fin >> input;
fin.close();
fout.open("outputFile.txt");
fout << input;
fout << "\n Data transferred \n";
fout.close();
}
This works perfectly when I build and run it, so I've got the proper path to the desktop folder set up. (I'm putting the data files in the same folder as the executable and specifying the path in Xcode.)
No problems when I run this within Xcode, but this is the message on the terminal console when I run the executable by itself:
"…/Desktop/datafiles/Build/Products/Debug/datafiles ; exit;
…/Desktop/datafiles/Build/Products/Debug/datafiles ; exit;
File failed to open.
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]"
Is there another flag or path that needs to be set within Xcode for this to work? Two other related questions: How do I access the terminal history to see what is going on? Finally, if I set up the project as an SFML app instead of a terminal project (command line tool app), why can't I see the files within the SFML app, even though I've set the command line flag to see hidden files, and I can see other hidden files on my hard drive? I can see the files if I open the SFML app folder in Windows, so I know they are there.
This is my first question on Stack Overflow, so apologies if this should be appended to the previous question, but this doesn't appear to be an answer to me, but is quite a different version of the original question that is not addressed in the answers.
Thanks!

How can I launch Mozilla Firefox using C++?

Compiled through compiler like Code::Blocks, I have tried the following, bit it does not work:
/*Running Firefox.exe*/
#include <stdio.h>
/*using c++*/
#include <iostream>
#include <stdlib.h>
using namespace std;
int main ()
{
int x;
cout << "Checking if processor is available..." << endl;
/*System used here*/
if (system(NULL)) puts ("Proceed");
else exit (1);
cout<< "Executing Firefox..." << endl;
/*Having some error here saying not recognized as internal or external command*/
x = system ("C:/Program Files (x86)/Mozilla Firefox/firefox.exe");
/*cout here*/
cout <<"The value returned was:" << x << endl;
return 0;
}
Is it because Firefox is not recognized as a system of windows? If so how can I run Firefox, or even Internet Explorer from code?
Run cmd.exe (Windows command shell) and enter the string C:/Program Files (x86)/Mozilla Firefox/firefox.exe at the command line, and you will see the same problem - i.e. the problem is with your command string rather than your C++ code.
The space in the path requires the command string to be quoted:
system ("\"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe\"") ;
or
system ("\"C:/Program Files (x86)/Mozilla Firefox/firefox.exe\"") ;
I believe you need to convert path to DOS format
Launch a command prompt cd into Mozilla Firefox folder
run this: for %I in (.) do echo %~sI
Copy the output to system command with \ replaced with \\ append firefox.exe at end
Is it because Firefox is not recognized as a system of windows?
If you open up cmd.exe and type in C:/Program Files... it won't work because spaces are used as a delimiter. Quote your path:
system("\"C:/Program Files (x86)/Mozilla Firefox/firefox.exe\"");
Although if you're targeting Windows you should consider using CreateProcess which saves you this trouble.
If so how can I run Firefox, or even Internet Explorer from code?
If you want to show a web page, use ShellExecute* and let the shell do the work. It will take care of launching Firefox, Internet Explorer, Chrome, or whatever browser the user has configured to view web pages.
*Read the Remarks section about initializing COM, first.
Try using windows API CreateProcess API