opengl tutorial xcode cmake - xcode

I followed every instruction on OpenGL-tutorial.org, downloading the 2.1 code, running it through cmake specifying xcode and using its default compilers. I configured till the red lines were gone then clicked generated. The project file it generated though, when opened in xcode displays the following errors:
/OpenGL-tutorial_v0010_21/external/AntTweakBar-1.15/src/LoadOGLCore.h:149:24: Conflicting types for 'glMultiDrawElements'
/OpenGL-tutorial_v0010_21/external/AntTweakBar-1.15/src/LoadOGLCore.h:214:24: Conflicting types for 'glShaderSource'
/external/AntTweakBar-ObjectiveC/LoadOGLCore.mm:116:1: Cannot initialize a variable of type 'PFNglMultiDrawElements' (aka 'void (*)(GLenum, const GLsizei *, GLenum, const GLvoid **, GLsizei)') with an lvalue of type 'void (GLenum, const GLsizei *, GLenum, const GLvoid *const *, GLsizei)': type mismatch at 4th parameter ('const GLvoid **' (aka 'const void **') vs 'const GLvoid *const *' (aka 'const void *const *'))
/external/AntTweakBar-ObjectiveC/LoadOGLCore.mm:116:1: Cannot initialize a variable of type 'PFNglMultiDrawElements' (aka 'void (*)(GLenum, const GLsizei *, GLenum, const GLvoid **, GLsizei)') with an lvalue of type 'void (GLenum, const GLsizei *, GLenum, const GLvoid *const *, GLsizei)': type mismatch at 4th parameter ('const GLvoid **' (aka 'const void **') vs 'const GLvoid *const *' (aka 'const void *const *'))
I am using Xcode 4.6.1 and cmake 2.8-10
Could this be something wrong with cmake or the tutorials code?

The first two errors are easy enough to fix, maybe it will fix the other two. Notice the signature in gl.h for glShaderSource. It is:
extern void glShaderSource (GLuint shader, GLsizei count, const GLchar* const *string, const GLint *length);
The third parameter is a 'const pointer to a const pointer to string.'
However, notice that the signature in LoadOGLCore.h is (expanding the macro):
extern "C" { void APIENTRY glShaderSource (GLuint shader, GLsizei count, const GLchar * *string, const GLint *length);
The third parameter is a 'const pointer to a string pointer' (missing the 'const'). So, open up LoadOGLCore.h and change (on line 214):
const GLchar* *string
to:
const GLchar* const *string
Do the same for the other error regarding glMultiDrawElements. This allowed me to at least build AntTweakBar on OSX 10.8.5. Still haven't tried to run a sample project yet, but I'll update when I do.

go to the definition of those functions and replace the prototype with the correct (update) parameter definitions

Author here,
Welcome on StackOverflow, and thanks for reading opengl-tutorial.org !
This is a known issue (on 10.8 only, which I don't have). However, AntTweakBar, which provides a simple GUI, is only used for the very last tutorial. Other sub-projects should build just fine. Just in case, you can remove lines 367 to 385 in the CMakeLists.txt file, and rerun CMake.
I'll update this post when it'll be fixed. Sorry for the inconvenience !

Related

Is const keyword mandatory while declaring"constexpr char*" string?

The project that I have just started working on has many instances of following,
constexpr const char* str = "Some Character(s)";
I wanted to understand, is the "const" keyword in above statement not redundant, as constexpr is implicitly constant?
It is mandatory because it won't compile if you remove it. This code:
constexpr char *str = "Some Character(s)";
Produces the following error on x64 GCC 11.2 (link):
error: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
1 | constexpr char *str = "Some Character(s)";
| ^~~~~~~~~~~~~~~~~~~
The implied const is for the pointer itself so a redundant const would actually be this:
constexpr const char *const str = "Some Character(s)";
// ^~~~~
const and constexpr has diferent behaivours, as both has the same prefix, you may think that they are the same, but constexpr means that an atribute (or function, or whatever) will be done in compile time, and const means that that atribute won't be modified (it is an inmutable value, you can modify it, but thats undefined behaivour), but maybe can't be evaluated at compile time.
Also, in that particular case, you can't create an char *from an string literal since -std=c++17

Why an argument can be outside of ( ) in a function definition?

I have a piece of code in my .y file as follows
void yyerror(s) char *s; {
fputs(s, stderr);
putc('\n', stderr);
}
The function prototype declaration is in another file as follows
void yyerror (char const *s);
I tried to compile the code on both Linux and mac. On Linux, the compiler generates the following error message
error: argument ‘s’ doesn’t match prototype
void yyerror(s) char *s; {
^
On mac, the compiler generates the following warning message
warning: promoted type 'char *' of K&R function parameter is not compatible with the parameter type 'const char *' declared in a previous prototype [-Wknr-promoted-parameter]
void yyerror(s) char *s; {
^
My questions are:
Why clang only generates warning while cc generates an error.
Both compilers complain about 's' in "char *s". Why they ignore the 's' in () but consider the second 's' as the argument?

decltype(*this) bug in VS2013?

While trying to formulate a C macro to ease the writing of non-const member functions calling const member functions with exact same logic (see Chapter 1, Item 3, "Avoiding Duplication in const and Non-const Member Functions" in Effective C++), I believe I came across a decltype() bug in VS2013 Update 1.
I wanted to use decltype(*this) to build a static_cast<decltype(*this) const&>(*this) expression in the aforementioned macro to avoid having the macro call site pass any explicit type information. However, that latter expression doesn't appear to properly add const in some cases in VS2013.
Here's a small block of code I was able to make repo the bug:
#include <stdio.h>
template<typename DatumT>
struct DynamicArray
{
DatumT* elements;
unsigned element_size;
int count;
inline const DatumT* operator [](int index) const
{
if (index < 0 || index >= count)
return nullptr;
return &elements[index];
}
inline DatumT* operator [](int index)
{
#if defined(MAKE_THIS_CODE_WORK)
DynamicArray const& _this = static_cast<decltype(*this) const&>(*this);
return const_cast<DatumT*>(_this[index]);
#else
// warning C4717: 'DynamicArray<int>::operator[]' : recursive on all control paths, function will cause runtime stack overflow
return const_cast<DatumT*>(
static_cast<decltype(*this) const>(*this)
[index]
);
#endif
}
};
int _tmain(int argc, _TCHAR* argv[])
{
DynamicArray<int> array = { new int[5], sizeof(int), 5 };
printf_s("%d", *array[0]);
delete array.elements;
return 0;
}
(may the first one to blab about not using std::vector be smitten)
You can either compile the above code and see the warning yourself, or refer to my lone comment to see what VC++ would spew at you. You can then ! the defined(MAKE_THIS_CODE_WORK) expression to have VC++ compile the code as how I'm excepting the #else code to work.
I don't have my trusty clang setup on this machine, but I was able to use GCC Explorer to see if clang complains (click to see/compile code). Which it doesn't. However, g++ 4.8 will give you an ‘const’ qualifiers cannot be applied to ‘DynamicArray&’ error message using that same code. So perhaps g++ also has a bug?
Referring to the decltype and auto standards paper (albeit, it's almost 11 years old), the very bottom of page 6 says that decltype(*this) in a non-const member function should be T&, so I'm pretty sure this should be legal...
So am I wrong in trying to use decltype() on *this plus adding const to it? Or is this a bug in VS2013? And apparently g++ 4.8, but in a different manner.
edit: Thanks to Ben Voigt's response I was able to figure out how to craft a standalone C macro for what I'm desire to do.
// Cast [this] to a 'const this&' so that a const member function can be invoked
// [ret_type] is the return type of the member function. Usually there's a const return type, so we need to cast it to non-const too.
// [...] the code that represents the member function (or operator) call
#define CAST_THIS_NONCONST_MEMBER_FUNC(ret_type, ...) \
const_cast<ret_type>( \
static_cast< \
std::add_reference< \
std::add_const< \
std::remove_reference< \
decltype(*this) \
>::type \
>::type \
>::type \
>(*this) \
__VA_ARGS__ \
)
// We can now implement that operator[] like so:
return CAST_THIS_NONCONST_MEMBER_FUNC(DatumT*, [index]);
The original desire was to hide this all in a macro, which is why I wasn't wanting to worry about creating typedefs or this aliases. It is still curious that clang in GCC Explorer didn't output a warning...though the output assembly does appear fishy.
You said yourself, decltype (*this) is T&. decltype (*this) const & tries to form a reference to a reference (T& const &). decltype triggers the reference collapsing rule 8.3.2p6. But it doesn't collapse the way you'd like.
You could say decltype(this) const&, but that would be T* const& -- a reference to a const pointer, not a pointer to a const object. For the same reason, decltype (*this) const and const decltype (*this) don't form const T&, but (T&) const. And top-level const on a reference is useless, since references already forbid rebinding.
Perhaps you are looking for something more like
const typename remove_reference<decltype(*this)>::type &
But note that you don't need the cast at all when adding const. Instead of
DynamicArray const& _this = static_cast<decltype(*this) const&>(*this);
just say
DynamicArray const& _this = *this;
These combine to
const typename std::remove_reference<decltype(*this)>::type & this_ = *this;
Still, this is a stupid amount of code for a very simple and pervasive problem. Just say:
const auto& this_ = *this;
FYI here's the text of the reference collapsing rule:
If a typedef-name (7.1.3, 14.1) or a decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a type T, an attempt to create the type "lvalue reference to cv TR" creates the type "lvalue reference to T", while an attempt to create the type "rvalue reference to cv TR" creates the type TR.
decltype(*this) is our decltype-specifier which denotes TR, which is DynamicArray<DatumT>&. Here, T is DynamicArray<DatumT>. The attempt TR const& is the first case, attempt to create lvalue reference to (const) TR, and therefore the final result is T&, not const T&. The cv-qualification is outside the innermost reference.
With regard to your macro
// Cast [this] to a 'const this&' so that a const member function can be invoked
// [ret_type] is the return type of the member function. Usually there's a const return type, so we need to cast it to non-const too.
// [...] the code that represents the member function (or operator) call
#define CAST_THIS_NONCONST_MEMBER_FUNC(ret_type, ...) \
const_cast<ret_type>( \
static_cast< \
std::add_reference< \
std::add_const< \
std::remove_reference< \
decltype(*this) \
>::type \
>::type \
>::type \
>(*this) \
__VA_ARGS__ \
)
It's much cleaner to do
// Cast [this] to a 'const this&' so that a const member function can be invoked
template<typename T> const T& deref_as_const(T* that) { return *that; }
// [ret_type] is the return type of the member function. Usually there's a const return type, so we need to cast it to non-const too.
// [...] the code that represents the member function (or operator) call
#define CAST_THIS_NONCONST_MEMBER_FUNC(ret_type, ...) \
const_cast<ret_type>(deref_as_const(this)__VA_ARGS__)
It's shorter, self-contained, compatible with C++98 except for __VA_ARGS__, and avoids an unnecessary cast

Signature difference in openGlShaderSource between OpenGL ES 2 revisions

I've noticed that the signature of openGLShaderSource has changed:
If you look at gl2.h from http://www.khronos.org/registry/gles/ (for me at rev 16803), the signature reads:
GL_APICALL void GL_APIENTRY glShaderSource (GLuint shader, GLsizei count, const GLchar* const* string, const GLint* length);
Now, on an older revision of that file (rev 10602), or when looking at the desktop version of that function, the signature reads:
GL_APICALL void GL_APIENTRY glShaderSource (GLuint shader, GLsizei count, const GLchar** string, const GLint* length);
note the missing "const" for the string parameter...
Has anyone an idea how one could detect this and keep backwards compatibility? As far as I can see, there is no "revision" macro defined, that one could check to use one version or the other.
Background information: I'm trying to compile WebKit for an embedded platform that ships the new gl2.h header. And webkit has this:
typedef void (*glShaderSourceType) (GLuint, GLsizei, const char**, const GLint*);
in http://opensource.apple.com/source/WebCore/WebCore-1298/platform/graphics/cairo/OpenGLShims.h and thus fails to compile when it tries to assign the glShaderSource function pointer to a glShaderSourceType-variable with the new gl2.h version...
Thanks to the hint by Nicol Bolas! This seems to compile:
void foo(const char* const*) {}
void bar(const char**) {}
typedef void (*ftype)(const char**);
int main() {
ftype f1 = reinterpret_cast(foo);
ftype f2 = bar;
return 0;
}

MFC CString to int

using this code i get an error at atoi's conversion from CString to int:
int nrCentrala; CString idCentrala;
GetDlgItem(IDC_EDIT_IDCENTRALA)->GetWindowText(idCentrala);
nrCentrala = atoi(&idCentrala);
where IDC_EDIT_IDCENTRALA is an edit control.
Error 7 error C2664: 'atoi' : cannot convert parameter 1 from 'CString *' to 'const char *'
CString provides an overloaded operator for LPCTSTR. Also, use _tstoi instead, just in case you're compiling with Unicode libraries, e.g.
nrCentrala = _tstoi(idCentrala);
You pass pointer to CString (CString* type) instead of const char* which is expected by atoi(). Correct call is nrCentrala = atoi(idCentrala.GetString());

Resources