receive Error code 500 when visiting a cgi file (in c++) - cgi

I wrote the following code in a file named test.cpp on godaddy web host:
#include <iostream>
using namespace std;
int main () {
cout << "Content-type:text/html\r\n\r\n";
cout << "<html>\n";
cout << "<head>\n";
cout << "<title>Hello World - First CGI Program</title>\n";
cout << "</head>\n";
cout << "<body>\n";
cout << "<h2>Hello World! This is my first CGI program</h2>\n";
cout << "</body>\n";
cout << "</html>\n";
return 0;
}
And I compile test.cpp on the godaddy host using "g++ test.cpp -o a.cgi".
Then I tried to access the cgi (type "www.XXX.com/a.cgi"), the error code 500 came out. I have no idea what went wrong.

Change the permissions of the file using file manager to make it executable
try www.xxx.com/cgi-bin/a.cgi
changing a.cgi to a.cpp and then check if it works

Related

Do not allow gcc to compile invalid dereference in debug mode

The code below tries to dereference the first entry of a zero size vector
#include <iostream>
#include <vector>
#define WATCH(a) std::cout << #a << ": " << a << std::endl;
int main(int argc, char** argv)
{
std::vector<int> A(0, 0);
WATCH(&A[0])
return 0;
}
This is invalid but compile and run with gcc-11.3.0 for both debug and release build. The build command in debug mode is
g++ -g -rdynamic main.o -o main
Is there a way to tell gcc to not allow this?

How to enable C++17 compiling in Visual Studio Community 2019 in a cmake project?

Before posting this question, I have read lots of threads published on StackOverflow. And I still did not find an answer that works.
I have seen that some people saty message is a duplicate, but it is not.
I have checked https://cmake.org/cmake/help/latest/prop_tgt/CXX_STANDARD.html#prop_tgt:CXX_STANDARD
But i want to apply that to the special case of MSVC, as MSVC is a special case.
I tried that in the out folder :
set_target_properties(myTarget PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)
For example, I have read:
How to enable C++17 in CMake
How to enable C++17 compiling in Visual Studio?
But these do not apply to my problem exactly.
Indeed, I recently uninstalled Visual Studio Community 2017, and I have installed Visual Studio Community 2019.
I wanted to compile a code coming from an idea of Bartłomiej Filipek.
The code is below. I used CMake. To the default CMake code, I have added the following:
if (MSVC_VERSION GREATER_EQUAL "1900")
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("/std:c++latest" _cpp_latest_flag_supported)
if (_cpp_latest_flag_supported)
add_compile_options("/std:c++latest")
endif()
endif()
And I have the following compile error:
Erreur C2039 'align_val_t' : not a member of 'std'
So, it means that C++17 is not taken into account.
The version of CMake which is the CMake file by default is indicated by:
cmake_minimum_required (VERSION 3.8)
But on some pages of StackOverflow say that flag is taken into account since CMake VERSION 3.10
However, VSC 19 proposes CMake 3.8
That's quite strange if it is the latest version.
So, how can I definitely solve the problem please?
#include <cstdint>
#include <iostream>
#include <malloc.h>
#include <cassert>
#include <vector>
#include <new>
void* operator new(std::size_t size, std::align_val_t align) {
//#ifdef _MSC_VER
#if defined(_WIN32) || defined(__CYGWIN__)
auto ptr = _aligned_malloc(size, static_cast<std::size_t>(align));
std::cout << "wind" << '\n';
#else
auto ptr = aligned_alloc(static_cast<std::size_t>(align), size);
#endif
if (!ptr)
throw std::bad_alloc{};
std::cout << "new: " << size << ", align: "
<< static_cast<std::size_t>(align)
<< ", ptr: " << ptr << '\n';
return ptr;
}
void operator delete(void* ptr, std::size_t size, std::align_val_t align)
{
std::cout << "avec size delete: " << size << ", align: "
<< static_cast<std::size_t>(align)
<< ", ptr : " << ptr << '\n';
//#ifdef _MSC_VER
#if defined(_WIN32) || defined(__CYGWIN__)
_aligned_free(ptr);
#else
free(ptr);
#endif
}
void operator delete(void* ptr, std::align_val_t align) {
std::cout << "delete: align: "
<< static_cast<std::size_t>(align)
<< ", ptr : " << ptr << '\n';
//#ifdef _MSC_VER
#if defined(_WIN32) || defined(__CYGWIN__)
_aligned_free(ptr);
#else
free(ptr);
#endif
}
//And here’s some test code:
class alignas(32) Vec3dAVX {
double x, y, z;
};
int main() {
std::cout << "std::vector\n";
std::vector<Vec3dAVX> vec;
vec.push_back({});
vec.push_back({});
vec.push_back({});
assert(reinterpret_cast<uintptr_t>(vec.data()) % alignof(Vec3dAVX) == 0);
}
The CMake code:
# CMakeList.txt : fichier projet CMake de niveau supérieur, effectuez une configuration globale
# et incluez les sous-projets ici.
#
cmake_minimum_required (VERSION 3.8)
project ("lameduck")
#Incluez les sous-projets.
add_subdirectory ("lameduck")
if (MSVC_VERSION GREATER_EQUAL "1900")
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("/std:c++latest" _cpp_latest_flag_supported)
if (_cpp_latest_flag_supported)
add_compile_options("/std:c++latest")
endif()
endif()

Missing <ofstream>: No such file or Directory

Running Ubuntu 12.04, using the g++ compiler and have the essential-build installed. Currently trying to get a small little program to work the code is below:
#include "Muon.h"
#include "Electron.h"
#include "Photon.h"
#include <string>
using std::string;
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <ofstream>
using std::ofstream;
#include <vector>
using std::vector;
using namespace std;
int main(){
cout << "Let's create a Particle vector!" << endl;
Electron* a = new Electron(10.0,20.0,30.0);
cout << "Electron created" << endl;
Muon* b = new Muon(30.0,20.0,10.0);
cout << "Muon created" << endl;
Photon* c = new Photon(20.0,10.0,30.0);
cout << "Photon created" << endl;
vector<Particle*> particlesContainer;
particlesContainer.push_back(a);
particlesContainer.push_back(b);
particlesContainer.push_back(c);
int size = particlesContainer.size();
cout << "size is: " << size << endl;
ofstream file("Muon_Vector.data");
if (file.is_open()){
for (int i = 0; i < size; i++){
file << particlesContainer[0]->getType << endl;
}
}
else {cout << "Unable to open file" << endl;}
file.close();
return 0;
}
Upon trying to compile I receive the error:
"intParticles.cpp:15:20: fatal error: ofstream: No such file or directory
compilation terminated."
This is after typing: "g++ -Wall Particle.cpp intParticles.cpp -o run"
Any help as to what is going wrong would be grateful. I thought ofstream was part of the standard library?
ofsteam is class but not the header file. It is defined in 'fstream' file

Two addresses of a function

What does GetProcAddress() do? According to some forums, it displays a functions address in a DLL.
When I run the above function with appropriate parameters, I get the address of printf as some address A, but if I do cout << printf, get a different address B.
Why?
cout << GetProcAddress(
GetModuleHandle(TEXT("MSVCRT.DLL")),
"printf");
cout << "****" << printf << endl;
#Raymond
yes you are right i was getting the linker addresses in the thunk table...
thank you..

What is missing in the CMakeLists.txt / my library installation?

I reduced my prog to the minimum just to show the pb: cmake does not complain, neither make (running Linux / libglew 1.5 / cmake 2.8.2). The program segfaults, and I have no clue why / which steps I should now make to solve the pb.
Source of render.cpp:
#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/glext.h>
#ifndef WIN32
#include <GL/glx.h>
#endif
#include <iostream>
#include <assert.h>
using namespace std;
int main()
{
cout << "Before init" << endl;
GLenum err = glewInit();
cout << "After init" << endl;
if (err != GLEW_OK)
cout << "Initialization error" << endl;
else{
cout << "Successful init" << endl;
assert(glCreateShader);
GLuint f = glCreateShader(GL_FRAGMENT_SHADER);
}
}
The content of CMakeList is:
cmake_minimum_required (VERSION 2.6)
project (render)
IF(CMAKE_COMPILER_IS_GNUCC)
SET(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -Wall -std=c++0x")
ENDIF(CMAKE_COMPILER_IS_GNUCC)
# Some directory shortcuts
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/conf)
INCLUDE(FindOpenGL REQUIRED)
INCLUDE(FindGLEW REQUIRED)
INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR})
INCLUDE_DIRECTORIES(${GLEW_INCLUDE_DIR})
message(${OPENGL_LIBRARIES})
message(${GLEW_INCLUDE_DIR})
message(${GLEW_LIBRARIES})
add_executable(render render.cpp)
target_link_libraries(render ${GLEW_LIBRARIES} ${OPENGL_LIBRARIES})
As mentioned, it compiles and segfaults at the call to glewInit(). I have no idea what to search for at this point..
Any help would be great,
Thanks
Might be useful to anyone, I solved it: using openGL, you need to create a context before being able to call glewInit(). This can be done using the SDL library (simplifies this task). Have a look at this tutorial.