cannot open file 'glew_static_vs2010.lib' and pragma comment - c++

I'm trying to get started with a book about modern OpenGL. In the book, I need to link against some libs (i.e. freeglut glew32 vermilion32). I've downloaded the source code for glew and generated the lib. There is no problem with linking the libs I've generated but it seems the author uses pragma comment and it causes some linking errors.
LINK : fatal error LNK1104: cannot open file 'glew_static_vs2010.lib'
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\cl.EXE"' : return code '0x2'
In one of headers files (i.e. vgl.h) used in the code, there are these lines
#ifndef __VGL_H__
#define __VGL_H__
// #define USE_GL3W
#ifdef USE_GL3W
#include <GL3/gl3.h>
#include <GL3/gl3w.h>
#else
#define GLEW_STATIC
#include <GL/glew.h>
#ifdef _MSC_VER
# ifdef _DEBUG
# if (_MSC_VER >= 1600)
# pragma comment (lib, "glew_static_vs2010_d.lib")
# else
# pragma comment (lib, "glew_static_d.lib")
# endif
# else
# if (_MSC_VER >= 1600)
# pragma comment (lib, "glew_static_vs2010.lib")
# else
# pragma comment (lib, "glew_static.lib")
# endif
# endif
#endif
#endif
#define FREEGLUT_STATIC
#include <GL/freeglut.h>
#ifdef _MSC_VER
# ifdef _DEBUG
# if (_MSC_VER >= 1600)
# pragma comment (lib, "freeglut_static_vs2010_d.lib")
# else
# pragma comment (lib, "freeglut_static_d.lib")
# endif
# else
# if (_MSC_VER >= 1600)
# pragma comment (lib, "freeglut_static_vs2010.lib")
# else
# pragma comment (lib, "freeglut_static.lib")
# endif
# endif
#endif
#define BUFFER_OFFSET(x) ((const void*) (x))
#endif /* __VGL_H__ */
I don't know why the author is following this approach. How can I overcome this problem? This is my Makefile
CC = cl
CFLAGS = /EHsc /c
INCLUDES = \
/I D:\CPP_Projects\CommandPrompt\ModernOpenGL\Books\Opengl_Programming_Guide_8th\oglpg_8th_edition\include
LIBS = \
/LIBPATH:D:\CPP_Projects\CommandPrompt\ModernOpenGL\Books\Opengl_Programming_Guide_8th\oglpg_8th_edition\lib \
freeglut_staticd.lib glew32sd.lib vermilion32_d.lib
all: project
project: triangles.obj LoadShaders.obj
$(CC) /EHsc /Fetest.exe triangles.obj LoadShaders.obj \
/link $(LIBS)
triangles.obj: triangles.cpp
$(CC) $(CFLAGS) triangles.cpp $(INCLUDES)
LoadShaders.obj: LoadShaders.cpp
$(CC) $(CFLAGS) LoadShaders.cpp $(INCLUDES)
clean:
del *.exe *.obj

Microsoft compiler allows to use #pragma comment (lib, "libraryname.lib") to specify libraries that should be used by linker instead of passing them to linker on command line. Since you build your own libraries perhaps you should comment out these entire blocks for auto-linking from vgl.h (or even delete them):
#ifndef __VGL_H__
#define __VGL_H__
// #define USE_GL3W
#ifdef USE_GL3W
#include <GL3/gl3.h>
#include <GL3/gl3w.h>
#else
#define GLEW_STATIC
#include <GL/glew.h>
// #ifdef _MSC_VER
// # ifdef _DEBUG
// # if (_MSC_VER >= 1600)
// # pragma comment (lib, "glew_static_vs2010_d.lib")
// # else
// # pragma comment (lib, "glew_static_d.lib")
// # endif
// # else
// # if (_MSC_VER >= 1600)
// # pragma comment (lib, "glew_static_vs2010.lib")
// # else
// # pragma comment (lib, "glew_static.lib")
// # endif
// # endif
// #endif
#endif
#define FREEGLUT_STATIC
#include <GL/freeglut.h>
// #ifdef _MSC_VER
// # ifdef _DEBUG
// # if (_MSC_VER >= 1600)
// # pragma comment (lib, "freeglut_static_vs2010_d.lib")
// # else
// # pragma comment (lib, "freeglut_static_d.lib")
// # endif
// # else
// # if (_MSC_VER >= 1600)
// # pragma comment (lib, "freeglut_static_vs2010.lib")
// # else
// # pragma comment (lib, "freeglut_static.lib")
// # endif
// # endif
// #endif
#define BUFFER_OFFSET(x) ((const void*) (x))
#endif /* __VGL_H__ */

Related

using #if to run a function

I'm trying to run the expression _setmode only if I'm using Windows, and setlocale only if I'm using Linux, but I can't manage to make them work with a simple if-else inside a function due to Linux having errors with the Windows libraries, and vice versa.
#if defined(_WIN32) || defined(_WIN64) || (defined(__CYGWIN__) && !defined(_WIN32))
#define PLATFORM_NAME 0
#include <io.h>
#include <fcntl.h>
#elif defined(__linux__)
#define PLATFORM_NAME 1
#include <locale>
#elif defined(__APPLE__) && defined(__MACH__)
#include <TargetConditionals.h>
#if TARGET_OS_MAC == 1
#define PLATFORM_NAME 2
#endif
#else
#define PLATFORM_NAME NULL
#endif
#if PLATFORM_NAME == 0
_setmode(_fileno(stdout), _O_U8TEXT);
#endif
#if PLATFORM_NAME == 1
setlocale(LC_CTYPE, "");
#endif
If you write OS-dependent* code (as in this case), you can manage it at compile-time**. To do this, we need two parts:
Define OS-dependent constants (optional, if condition simple, this part can be omitted):
#if defined(_WIN32)
#define PLATFORM_NAME 0
#include <fcntl.h>
#include <io.h>
#elif defined(__linux__)
#define PLATFORM_NAME 1
#include <locale>
#endif
In needed place call OS-dependent code with preprocessor conditions:
#if PLATFORM_NAME == 0
//Windows code here
#endif
You can write more complex conditions:
#if PLATFORM_NAME == 0
//Windows code here
#elif PLATFORM_NAME != 0
//Non-Windows code here
#if PLATFORM_NAME == 1 || PLATFORM_NAME == 2
//Linux or unknown OS code here
#endif
#endif
See restrictions of conditions here
Tip: If your code have entrypoint (main function as example), you can call most of OS-dependent code at main if this help to reduce code. In library you can place OS-dependent code to dedicated source-file functions like there. Usage preprocessor-time code is good method for writing zero-cost runtime code because of preprocessor just remove all sources,if they're not met conditions.
* - or whatever-dependent 😃
** - more precisely, preprocessor-time
Source: GNU, Microsoft docs.

Chromium `debugger` equivalent, on `gdb` for Cygwin?

How do people trigger a breakpoint on gdb (for Cygwin, specifically) from the very source code?
Like when a JS script has the debugger word in it and Chromium dev tools trigger stop for debugging?
Here's how SDL2 implements this feature:
#if defined(_MSC_VER)
/* Don't include intrin.h here because it contains C++ code */
extern void __cdecl __debugbreak(void);
#define SDL_TriggerBreakpoint() __debugbreak()
#elif ( (!defined(__NACL__)) && ((defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))) )
#define SDL_TriggerBreakpoint() __asm__ __volatile__ ( "int $3\n\t" )
#elif defined(__386__) && defined(__WATCOMC__)
#define SDL_TriggerBreakpoint() { _asm { int 0x03 } }
#elif defined(HAVE_SIGNAL_H) && !defined(__WATCOMC__)
#include <signal.h>
#define SDL_TriggerBreakpoint() raise(SIGTRAP)
#else
/* How do we trigger breakpoints on this platform? */
#define SDL_TriggerBreakpoint()
#endif
The conditionals should probably resolve to __asm__ __volatile__ ( "int $3\n\t" ) on Cygwin.

Error for structure definition in Visual C++

I am facing problem in structure definition which worked fine using gcc
now I want to compile same program in visual studio but some of the conventions are not matching. I have done some changes to the code but I can't figure it out where I am going wrong.
below code is from brg_types.h
#ifndef _BRG_TYPES_H
#define _BRG_TYPES_H
#if defined(__cplusplus)
extern "C" {
#endif
#include <limits.h>
#if defined( _MSC_VER ) && ( _MSC_VER >= 1300 )
# include <stddef.h>
# define ptrint_t intptr_t
#elif defined( __GNUC__ ) && ( __GNUC__ >= 3 )
# include <stdint.h>
# define ptrint_t intptr_t
#else
# define ptrint_t int
#endif
#ifndef BRG_UI8
# define BRG_UI8
# if UCHAR_MAX == 255u
typedef unsigned char uint_8t;
# else
# error Please define uint_8t as an 8-bit unsigned integer type in brg_types.h
# endif
#endif
#ifndef BRG_UI16
# define BRG_UI16
# if USHRT_MAX == 65535u
typedef unsigned short uint_16t;
# else
# error Please define uint_16t as a 16-bit unsigned short type in brg_types.h
# endif
#endif
#ifndef BRG_UI32
# define BRG_UI32
# if UINT_MAX == 4294967295u
# define li_32(h) 0x##h##u
typedef unsigned int uint_32t;
# elif ULONG_MAX == 4294967295u
# define li_32(h) 0x##h##ul
typedef unsigned long uint_32t;
# elif defined( _CRAY )
# error This code needs 32-bit data types, which Cray machines do not provide
# else
# error Please define uint_32t as a 32-bit unsigned integer type in brg_types.h
# endif
#endif
#ifndef BRG_UI64
# if defined( __BORLANDC__ ) && !defined( __MSDOS__ )
# define BRG_UI64
# define li_64(h) 0x##h##ui64
typedef unsigned __int64 uint_64t;
# elif defined( _MSC_VER ) && ( _MSC_VER < 1300 ) /* 1300 == VC++ 7.0 */
# define BRG_UI64
# define li_64(h) 0x##h##ui64
typedef unsigned __int64 uint_64t;
# elif defined( __sun ) && defined( ULONG_MAX ) && ULONG_MAX == 0xfffffffful
# define BRG_UI64
# define li_64(h) 0x##h##ull
typedef unsigned long long uint_64t;
# elif defined( __MVS__ )
# define BRG_UI64
# define li_64(h) 0x##h##ull
typedef unsigned int long long uint_64t;
# elif defined( UINT_MAX ) && UINT_MAX > 4294967295u
# if UINT_MAX == 18446744073709551615u
# define BRG_UI64
# define li_64(h) 0x##h##u
typedef unsigned int uint_64t;
# endif
# elif defined( ULONG_MAX ) && ULONG_MAX > 4294967295u
# if ULONG_MAX == 18446744073709551615ul
# define BRG_UI64
# define li_64(h) 0x##h##ul
typedef unsigned long uint_64t;
# endif
# elif defined( ULLONG_MAX ) && ULLONG_MAX > 4294967295u
# if ULLONG_MAX == 18446744073709551615ull
# define BRG_UI64
# define li_64(h) 0x##h##ull
typedef unsigned long long uint_64t;
# endif
# elif defined( ULONG_LONG_MAX ) && ULONG_LONG_MAX > 4294967295u
# if ULONG_LONG_MAX == 18446744073709551615ull
# define BRG_UI64
# define li_64(h) 0x##h##ull
typedef unsigned long long uint_64t;
# endif
# endif
#endif
#if !defined( BRG_UI64 )
# if defined( NEED_UINT_64T )
# error Please define uint_64t as an unsigned 64 bit type in brg_types.h
# endif
#endif
#ifndef RETURN_VALUES
# define RETURN_VALUES
# if defined( DLL_EXPORT )
# if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )
# define VOID_RETURN __declspec( dllexport ) void __stdcall
# define INT_RETURN __declspec( dllexport ) int __stdcall
# elif defined( __GNUC__ )
# define VOID_RETURN __declspec( __dllexport__ ) void
# define INT_RETURN __declspec( __dllexport__ ) int
# else
# error Use of the DLL is only available on the Microsoft, Intel and GCC compilers
# endif
# elif defined( DLL_IMPORT )
# if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )
# define VOID_RETURN __declspec( dllimport ) void __stdcall
# define INT_RETURN __declspec( dllimport ) int __stdcall
# elif defined( __GNUC__ )
# define VOID_RETURN __declspec( __dllimport__ ) void
# define INT_RETURN __declspec( __dllimport__ ) int
# else
# error Use of the DLL is only available on the Microsoft, Intel and GCC compilers
# endif
# elif defined( __WATCOMC__ )
# define VOID_RETURN void __cdecl
# define INT_RETURN int __cdecl
# else
# define VOID_RETURN void
# define INT_RETURN int
# endif
#endif
#define ALIGN_OFFSET(x,n) (((ptrint_t)(x)) & ((n) - 1))
#define ALIGN_FLOOR(x,n) ((uint_8t*)(x) - ( ((ptrint_t)(x)) & ((n) - 1)))
#define ALIGN_CEIL(x,n) ((uint_8t*)(x) + (-((ptrint_t)(x)) & ((n) - 1)))
#define UI_TYPE(size) uint_##size##t
#define UNIT_TYPEDEF(x,size) typedef UI_TYPE(size) x
#define BUFR_TYPEDEF(x,size,bsize) typedef UI_TYPE(size) x[bsize / (size >> 3)]
#define UNIT_CAST(x,size) ((UI_TYPE(size) )(x))
#define UPTR_CAST(x,size) ((UI_TYPE(size)*)(x))
#define u8 uint_8t
#define u32 uint_32t
#define u64 uint_64t
#if defined(__cplusplus)
}
#endif
#endif
Below code is within tables.h
#ifndef __tables_h
#define __tables_h
#include "brg_endian.h"
#define NEED_UINT_64T
//#include "brg_types.h"
#if (PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN)
const u32 T[8*256] __attribute__((aligned(64))) {
/*some code*/
};
#endif /* IS_BIG_ENDIAN */
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
const u32 T[8*256] __attribute__((aligned(64))) = {
/*some code*/
};
#endif /* IS_LITTLE_ENDIAN */
#endif /* __tables_h */
Above code got compiled with gcc compiler
in visual studio use of brg_types.h is done as it is
and tables.h was changed following
#pragma once
#ifndef __tables_h
#define __tables_h
#include "brg_endian.h"
#include "brg_types.h"
#if defined(_MSC_VER)
#define ALIGNED_(x) __declspec(align(x))
#else
#if defined(__GNUC__)
#define ALIGNED_(x) __attribute__ ((aligned(x)))
#endif
#endif
#define ALIGNED_TYPE_(t,x) typedef t ALIGNED_(x)
#pragma pack(1)
#define int8_t __int8
#define int16_t __int16
#define int32_t __int32
#define int64_t __int64
#define uint8_t unsigned __int8
#define uint16_t unsigned __int16
#define uint32_t unsigned __int32
#define uint64_t unsigned __int64
#if (PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN)
const uint32_t T[8*256] ALIGNED_(64) = {
/*some code*/
};
#endif /* IS_BIG_ENDIAN */
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
const uint32_t T[8*256] ALIGNED_(64) = {
};
#endif /* IS_LITTLE_ENDIAN */
#endif /* __tables_h */
Error thrown to me by visual studio is
Error 3 error C1903: unable to recover from previous error(s); stopping compilation
Error 1 error C2144: syntax error : 'int' should be preceded by ';'
Error 2 error C2513: 'int' : no variable declared before '='
near: const uint32_t T[8*256] ALIGNED_(64) = {
/*some code*/
};
Your problem is the ALIGNED_ macro. As it is currently defined, it needs to come before the declaration in VS, but after for GCC. The syntax you want to achieve is:
const uint32_t __declspec(align(64)) T[8*256] = // MSVC
const uint32_t T[8*256] __attribute__ ((aligned(64))) = // GCC
I think the solution is to pass the variable declaration to ALIGNED_, so that the usage looks like:
const uint32_t ALIGNED_(T[8*256], 64) = ...
The definition should look something like:
#if defined(_MSC_VER)
# define ALIGNED_(v, x) __declspec(align(x)) v
#elif defined(__GNUC__)
# define ALIGNED_(v, x) v __attribute__ ((aligned(x)))
#else
# error "Don't know how to define ALIGNED_"
#endif
Warning: This code has not been seen by any compiler. Assume it contains errors.
I don't know how to handle the definition of ALIGNED_TYPE because I can't see an example of its use.

Find C++ platform target at compile time in visual studio

I am building my solution for x86 and x64 platforms.
Does Visual Studio have any target platform variables so I find which platform I am building for in compile time?
For example:
HINSTANCE hinstLib;
#ifdef TARGET_X86
hinstLib = LoadLibrary("32lib.dll");
#endif
#ifdef TARGET_X64
hinstLib = LoadLibrary("64lib.dll");
#endif
This is what I use:
#if defined(_MSC_VER)
// Microsoft VC compiler
# if defined(_WIN32)
# if defined(_WIN64)
// 64 bit windows
# else
// 32 bit windows
# endif
# endif
#endif
Note that _WIN32 is defined for 64 bit too.
Have a look here: http://msdn.microsoft.com/en-US/library/b0084kay.aspx
_WIN64 or _M_X64 should work.
So for your example:
HINSTANCE hinstLib;
#ifdef _WIN64
hinstLib = LoadLibrary("64lib.dll");
#else
hinstLib = LoadLibrary("32lib.dll");
#endif

Microsoft C++ Pre-defined Macros [duplicate]

This question already has answers here:
How to find out cl.exe's built-in macros
(7 answers)
Closed 6 years ago.
Is there a cl.exe option to dump all the pre-defined Macros ( along with the defined values ).
Something like gcc -dM -E - < /dev/null for gcc.
Unfortunately, I don't think MSVC has a built in way of doing this.
I've used the following program to dump values of 'known' predefined symbols. I should give attribution (because I know I didn't come up with this), but I don't have notes on where I got it from (update: looks like I probably got it from here: http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/644c12ed-e3a7-4c5a-a73a-610fcc7913ca)...
#define __STR2__(x) #x
#define __STR1__(x) __STR2__(x)
#define __PPOUT__(x) "#define " #x " " __STR1__(x)
#if defined(_ATL_VER)
#pragma message(__PPOUT__(_ATL_VER ))
#endif
#if defined(_CHAR_UNSIGNED )
#pragma message(__PPOUT__(_CHAR_UNSIGNED ))
#endif
#if defined(__CLR_VER )
#pragma message(__PPOUT__(__CLR_VER ))
#endif
#if defined(__cplusplus_cli )
#pragma message(__PPOUT__(__cplusplus_cli ))
#endif
#if defined(__COUNTER__ )
#pragma message(__PPOUT__(__COUNTER__ ))
#endif
#if defined(__cplusplus )
#pragma message(__PPOUT__(__cplusplus ))
#endif
#if defined(_CPPLIB_VER )
#pragma message(__PPOUT__(_CPPLIB_VER ))
#endif
#if defined(_CPPRTTI )
#pragma message(__PPOUT__(_CPPRTTI ))
#endif
#if defined(_CPPUNWIND )
#pragma message(__PPOUT__(_CPPUNWIND ))
#endif
#if defined(_DEBUG )
#pragma message(__PPOUT__(_DEBUG ))
#endif
#if defined(_DLL )
#pragma message(__PPOUT__(_DLL ))
#endif
#if defined(__FUNCDNAME__ )
#pragma message(__PPOUT__(__FUNCDNAME__ ))
#endif
#if defined(__FUNCSIG__ )
#pragma message(__PPOUT__(__FUNCSIG__ ))
#endif
#if defined(__FUNCTION__ )
#pragma message(__PPOUT__(__FUNCTION__ ))
#endif
#if defined(_INTEGRAL_MAX_BITS )
#pragma message(__PPOUT__(_INTEGRAL_MAX_BITS ))
#endif
#if defined(_M_ALPHA )
#pragma message(__PPOUT__(_M_ALPHA ))
#endif
#if defined(_M_CEE )
#pragma message(__PPOUT__(_M_CEE ))
#endif
#if defined(_M_CEE_PURE )
#pragma message(__PPOUT__(_M_CEE_PURE ))
#endif
#if defined(_M_CEE_SAFE )
#pragma message(__PPOUT__(_M_CEE_SAFE ))
#endif
#if defined(_M_IX86 )
#pragma message(__PPOUT__(_M_IX86 ))
#endif
#if defined(_M_IA64 )
#pragma message(__PPOUT__(_M_IA64 ))
#endif
#if defined(_M_IX86_FP )
#pragma message(__PPOUT__(_M_IX86_FP ))
#endif
#if defined(_M_MPPC )
#pragma message(__PPOUT__(_M_MPPC ))
#endif
#if defined(_M_MRX000 )
#pragma message(__PPOUT__(_M_MRX000 ))
#endif
#if defined(_M_PPC )
#pragma message(__PPOUT__(_M_PPC ))
#endif
#if defined(_M_X64 )
#pragma message(__PPOUT__(_M_X64 ))
#endif
#if defined(_MANAGED )
#pragma message(__PPOUT__(_MANAGED ))
#endif
#if defined(_MFC_VER )
#pragma message(__PPOUT__(_MFC_VER ))
#endif
#if defined(_MSC_BUILD )
#pragma message(__PPOUT__(_MSC_BUILD ))
#endif
#if defined(_MSC_EXTENSIONS )
#pragma message(__PPOUT__(_MSC_EXTENSIONS ))
#endif
#if defined(_MSC_FULL_VER )
#pragma message(__PPOUT__(_MSC_FULL_VER ))
#endif
#if defined(_MSC_VER )
#pragma message(__PPOUT__(_MSC_VER ))
#endif
#if defined(__MSVC_RUNTIME_CHECKS )
#pragma message(__PPOUT__(__MSVC_RUNTIME_CHECKS ))
#endif
#if defined(_MT )
#pragma message(__PPOUT__(_MT ))
#endif
#if defined(_NATIVE_WCHAR_T_DEFINED)
#pragma message(__PPOUT__(_NATIVE_WCHAR_T_DEFINED))
#endif
#if defined(_OPENMP )
#pragma message(__PPOUT__(_OPENMP ))
#endif
#if defined(_VC_NODEFAULTLIB )
#pragma message(__PPOUT__(_VC_NODEFAULTLIB ))
#endif
#if defined(_WCHAR_T_DEFINED )
#pragma message(__PPOUT__(_WCHAR_T_DEFINED ))
#endif
#if defined(_WIN32 )
#pragma message(__PPOUT__(_WIN32 ))
#endif
#if defined(_WIN64 )
#pragma message(__PPOUT__(_WIN64 ))
#endif
#if defined(_Wp64 )
#pragma message(__PPOUT__(_Wp64 ))
#endif
void main() {}
I don't see that option in the MSDN documentation, but the list of predefined macros for Visual Studio 2008 and 2010 are available.