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?
Related
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()
I'm trying to compile the following code
#include <iostream>
#include <windows.h>
#include <objbase.h>
int main (int argc, char** argv) {
HRESULT hr;
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (SUCCEEDED(hr)) {
std::cout << "Initialized" << std::endl;
} else {
std::cout << "Failed" << std::endl;
}
CoUninitialize();
return 0;
}
but
g++ -o test -L"<dir>" -lOle32 <file>.cpp
# <dir> contains Ole32.Lib
always tells me that __imp_CoInitializeEx and __imp_CoUninitialize are undefined and -print-file-name=Ole32.Lib just return Ole32.Lib. If g++ doesn't find Ole32.Lib, maybe
g++ -c -o test.o <file>.cpp
ld -L"<dir>" -lOle32 -o test test.o
works. Now g++/ld actually finds CoInitializeEx and CoUninitialize, but the standard library seems to be missing and adding -static-libstdc++ or -lstdc++ or -llibstdc++ doesn't help either. So what am I missing? Why is g++ unable to find CoInitializeEx and CoUninitialize?
EDIT: I can definitely say that there is nothing wrong with my code, my header files and my library files, because I can compile the code using Visual Studios compiler:
cl /c /EHsc ^
/I"<...>\Microsoft Visual Studio 14.0\VC\include" ^
/I"<...>\Windows Kits\10\Include\<version>\ucrt" ^
/I"<...>\Windows Kits\10\Include\<version>\shared" ^
/I"<...>\Windows Kits\10\Include\<version>\um" ^
/Fotest.obj ^
main.cpp
link /nologo /machine:x64 /subsystem:console ^
/libpath:"<...>\Microsoft Visual Studio 14.0\VC\lib\amd64" ^
/libpath:"<...>\Windows Kits\10\Lib\<version>\ucrt\x64" ^
/libpath:"<...>\Windows Kits\10\Lib\<version>\um\x64" ^
/out:test.exe ^
test.obj Ole32.Lib
#include <iostream>
using namespace std;
int main()
{
constexpr double GetPi() {return 22.0/7;}
cout << GetPi() << endl;
}
No idea what is going on here. Please help me.
I've tried to compile this and even tried to re-format the code but it doesn't really help much. I thought this was the correct way you did things? What's going on here? It's a constant expression.
Here is the compilation error:
C:\Windows\system32\cmd.exe /C "C:/TDM-GCC-64/bin/mingw32-make.exe -j4` SHELL=cmd.exe -e -f Makefile"
"----------Building project:[ Constant_Expression - Debug ]----------"
mingw32-make.exe[1]: Entering directory 'C:/Program Files/CodeLite/CPP/Constants/Constant_Expression'
codelite-cc C:/TDM-GCC-64/bin/g++.exe -c "C:/Program Files/CodeLite/CPP/Constants/Constant_Expression/main.cpp" -g -O0 -std=c++11 -Wall -o ./Debug/main.cpp.o -I. -I.
C:/Program Files/CodeLite/CPP/Constants/Constant_Expression/main.cpp: In function 'int main()':
C:/Program Files/CodeLite/CPP/Constants/Constant_Expression/main.cpp:7:30: error: a function-definition is not allowed here before '{' token
constexpr double GetPi() {return 22.0/7;}
^
C:/Program Files/CodeLite/CPP/Constants/Constant_Expression/main.cpp:8:19: error: 'GetPi' was not declared in this scope
cout << GetPi() << endl;
^
mingw32-make.exe[1]: *** [Debug/main.cpp.o] Error 1
Constant_Expression.mk:93: recipe for target 'Debug/main.cpp.o' failed
mingw32-make.exe[1]: Leaving directory 'C:/Program Files/CodeLite/CPP/Constants/Constant_Expression'
mingw32-make.exe: *** [All] Error 2
Makefile:4: recipe for target 'All' failed
3 errors, 0 warnings
In C++ you are not allowed to define a function inside another function. (unlike closures in javascript).
You have to define GetPI() outside the main() function
#include <iostream>
using namespace std;
constexpr double GetPi() {return 22.0/7;}
int main()
{
cout << GetPi() << endl;
}
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
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.