unresolved external symbol _WinMain#16 - ogre

I tried compiling the following code (From Ogre3d Beginner's Guide By Felix Kerger book)
#include "Ogre\ExampleApplication.h"
class Example1 : public ExampleApplication
{
public:
void createScene()
{Ogre::Entity* ent =
mSceneMgr->createEntity("MyEntity","Sinbad.mesh");
mSceneMgr->getRootSceneNode()->attachObject(ent);
}
};
int main (void)
{
Example1 app;
app.go();
return 0;
}
So the compiled Process didn't work. I am currently using Visual Studio 2010 and OgreSDK_vc10_v1-7-3.
The errors are
1>InitializeBuildStatus:
1> Touching "obj\Debug\OgreApp3.unsuccessfulbuild".
1>ClCompile:
1> main.cpp
1>d:\ogresdk_vc10_v1-7-3\include\ogre\exampleframelistener.h(343): warning C4244: '+=': conversion from 'double' to 'Ogre::Real', possible loss of data
1>d:\ogresdk_vc10_v1-7-3\include\ogre\exampleframelistener.h(344): warning C4244: '-=' : conversion from 'double' to 'Ogre::Real', possible loss of data
1>d:\ogresdk_vc10_v1-7-3\include\ogre\exampleframelistener.h(348): warning C4244: 'argument' : conversion from 'double' to 'Ogre::Real', possible loss of data
1>d:\ogresdk_vc10_v1-7-3\include\ogre\exampleframelistener.h(349): warning C4244: 'argument' : conversion from 'double' to 'Ogre::Real', possible loss of data
1>d:\ogresdk_vc10_v1-7-3\include\ogre\exampleframelistener.h(454): warning C4244: '-=' : conversion from 'double' to 'Ogre::Real', possible loss of data
1>ManifestResourceCompile:
1> All outputs are up-to-date.
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain#16 referenced in function ___tmainCRTStartup
1>bin\Debug\\OgreApp3.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:10.57
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Replace the signature of your main function to the following:
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )

right bottom on your project
->property linker->subsystem
change your subsystem with "CONSOLE(/SUBSYSTEM:CONSOLE)"
like this https://www.dropbox.com/s/li3qnzw152snizo/ogre.png

Your compiler thinks the program is a windows application but your source code is for a command line application.

Related

compile error in reactive extensions for cpp

I encounter a strange question about rxcpp.
When capturing a rxcpp::connectable_observable variable and call it's connect method, I got a compile error as follow.
1>------ Build started: Project: LearnRx, Configuration: Debug x64 ------
1>Main.cpp
1>c:\users\liuzichao\source\repos\learnrx\learnrx\main.cpp(19): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'std::thread::id'
1>c:\users\liuzichao\source\repos\learnrx\learnrx\main.cpp(259): error C2662: 'rxcpp::composite_subscription rxcpp::connectable_observable<SourceValue,Multicast>::connect(rxcpp::composite_subscription)': cannot convert 'this' pointer from 'const Result' to 'rxcpp::connectable_observable<SourceValue,Multicast> &'
1> with
1> [
1> SourceValue=int,
1> Multicast=rxcpp::operators::detail::multicast<int,rxcpp::observable<int,rxcpp::sources::detail::create<int,publish_example::<lambda_3a462b77ca22ce68ef79403bfe94c65d>>>,rxcpp::subjects::subject<int>>
1> ]
1> and
1> [
1> Result=rxcpp::connectable_observable<int,rxcpp::operators::detail::multicast<int,rxcpp::observable<int,rxcpp::sources::detail::create<int,publish_example::<lambda_3a462b77ca22ce68ef79403bfe94c65d>>>,rxcpp::subjects::subject<int>>>
1> ]
1> and
1> [
1> SourceValue=int,
1> Multicast=rxcpp::operators::detail::multicast<int,rxcpp::observable<int,rxcpp::sources::detail::create<int,publish_example::<lambda_3a462b77ca22ce68ef79403bfe94c65d>>>,rxcpp::subjects::subject<int>>
1> ]
1>c:\users\liuzichao\source\repos\learnrx\learnrx\main.cpp(259): note: Conversion loses qualifiers
1>c:\users\liuzichao\source\repos\learnrx\learnrx\main.cpp(334): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'std::thread::id'
1>c:\users\liuzichao\source\repos\learnrx\learnrx\main.cpp(338): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'std::thread::id'
1>c:\users\liuzichao\source\repos\learnrx\learnrx\main.cpp(345): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'std::thread::id'
1>c:\users\liuzichao\source\repos\learnrx\learnrx\main.cpp(346): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'std::thread::id'
1>c:\users\liuzichao\source\repos\learnrx\learnrx\main.cpp(348): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'std::thread::id'
1>Done building project "LearnRx.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The problem code is shown below.
void publish_example() {
auto o = rxcpp::observable<>::create<int>([](rxcpp::subscriber<int> s) {
s.on_next(0);
s.on_next(1);
}).publish();
o.subscribe([](int v) {printf("%d", v); });
rxcpp::observable<>::timer(std::chrono::microseconds(2000)).subscribe(
[](const int i) {},
[](const std::exception_ptr& e) {},
[o]() {
o.connect();
}
);
}
I tried 2 methods to pass compiling
1. Comment "o.connect()"
1. turn variable o to static variable, so o doesn't need to be captured. code is shown follow
void publish_example() {
static auto o = rxcpp::observable<>::create<int>([](rxcpp::subscriber<int> s) {
s.on_next(0);
s.on_next(1);
}).publish();
o.subscribe([](int v) {printf("%d", v); });
rxcpp::observable<>::timer(std::chrono::microseconds(2000)).subscribe(
[](const int i) {},
[](const std::exception_ptr& e) {},
[]() {
o.connect();
}
);
}
Could you help telling me why the compile error appeared and hwo to figure it?
Thank you!
I figured this out.
What happend in here is because connectable_observable::connect is not a const method. After change it to a const method, the compile error disappeared.

When I use MSBuild to build an Android Makefile project and print log, I can't put string "error" and ":" together

I wrote a script to build an Android NDK application, and the source is below:
#include <stdio.h>
#include <stdlib.h>
#define LOG_ID_RADIO 1
#define ANDROID_LOG_DEBUG 3
#define LOG_TAG "TEST_LOG"
#define LOGD(...) printf(ANDROID_LOG_DEBUG,LOG_TAG ,VA_ARGS);
int main()
{
size_t datalen = 0;
LOGD("data length error: %d", datalen); <-- Please notice the string: "error:"
return 0;
}
The script is below:
cd C:/workspace/ndkTest/ndk-build/TestCC/jni
echo ====
call ndk-build -C C:/workspace/ndkTest/ndk-build/TestCC/jni -B
set path=C:\ProgramData\Microsoft\AndroidNDK64\android-ndk-r15c;%path%
echo error=%ERRORLEVEL%
pause
Reproduce steps:
Download the project: testcc.zip, extract this .zip file, remember the build.bat location, for example: my build.bat location.
Open the build.bat file, maybe you need change two things:
C:/workspace/ndkTest/ndk-build/TestCC/jni <-- build.bat location
C:\ProgramData\Microsoft\AndroidNDK64\android-ndk-r15c <-- NDK Location
In Visual Studio, create an Android Makefile Project, right click this project and select Properties, the default setting, find NMake → General → Build Command Line, change it to your build.bat location, like this.
Build this solution.
When I use the string error:,
LOGD("data length error: %d", datalen);
this project will result in build failed. Error log:
1>------ Build started: Project: Project14, Configuration: Debug Win32 ------
1>====
1>make: Entering directory `C:/workspace/ndkTest/ndk-build/TestCC/jni'
1>[arm64-v8a] Compile : testtime <= timetest.c
1>C:/workspace/ndkTest/ndk-build/TestCC/jni/./timetest.c(16,2): warning G5552ABC2: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Wint-conversion]
1>EXEC : LOGD("data length error : %d", datalen);
1> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1>C:/workspace/ndkTest/ndk-build/TestCC/jni/./timetest.c:10:26: note: expanded from macro 'LOGD'
1>#define LOGD(...) printf(ANDROID_LOG_DEBUG,LOG_TAG ,__VA_ARGS__);
1> ^~~~~~~~~~~~~~~~~
1>C:/workspace/ndkTest/ndk-build/TestCC/jni/./timetest.c:7:27: note: expanded from macro 'ANDROID_LOG_DEBUG'
1>#define ANDROID_LOG_DEBUG 3
1> ^
1>C:/Users/v-shenya/AppData/Local/Android/Sdk/ndk-bundle/build//../sysroot/usr/include\stdio.h:129:24: note: passing argument to parameter '__fmt' here
1>int printf(const char* __fmt, ...) __printflike(1, 2);
1> ^
1>1 warning generated.
1>[arm64-v8a] Executable : testtime
1>[arm64-v8a] Install : testtime => libs/arm64-v8a/testtime
1>make: Leaving directory `C:/workspace/ndkTest/ndk-build/TestCC/jni'
1>error=0
1>Press any key to continue . . .
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\VC\VCTargets\Microsoft.MakeFile.Targets(44,5): error MSB3073: The command "C:\workspace\ndkTest\ndk-build\TestCC\jni\build.bat" exited with code -1.
1>Done building project "Project14.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
But when I change error: to error (trailing space) or errdor:, it just doesn't put "error" and ":" together:
LOGD("data length errdor: %d", datalen); or
LOGD("data length error %d", datalen);
This project will build with success:
1>------ Build started: Project: Project14, Configuration: Debug Win32 ------
1>====
1>make: Entering directory `C:/workspace/ndkTest/ndk-build/TestCC/jni'
1>[arm64-v8a] Compile : testtime <= timetest.c
1>C:/workspace/ndkTest/ndk-build/TestCC/jni/./timetest.c(16,2): warning G5552ABC2: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Wint-conversion]
1> LOGD("data length error %d", datalen);
1> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1>C:/workspace/ndkTest/ndk-build/TestCC/jni/./timetest.c:10:26: note: expanded from macro 'LOGD'
1>#define LOGD(...) printf(ANDROID_LOG_DEBUG,LOG_TAG ,__VA_ARGS__);
1> ^~~~~~~~~~~~~~~~~
1>C:/workspace/ndkTest/ndk-build/TestCC/jni/./timetest.c:7:27: note: expanded from macro 'ANDROID_LOG_DEBUG'
1>#define ANDROID_LOG_DEBUG 3
1> ^
1>C:/Users/v-shenya/AppData/Local/Android/Sdk/ndk-bundle/build//../sysroot/usr/include\stdio.h:129:24: note: passing argument to parameter '__fmt' here
1>int printf(const char* __fmt, ...) __printflike(1, 2);
1> ^
1>1 warning generated.
1>[arm64-v8a] Executable : testtime
1>[arm64-v8a] Install : testtime => libs/arm64-v8a/testtime
1>make: Leaving directory `C:/workspace/ndkTest/ndk-build/TestCC/jni'
1>error=0
1>Press any key to continue . . .
1>Done building project "Project14.vcxproj".
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
My Visual Studio 2017 version is 15.6.6. Is this a bug of MSBuild? How can I fix this?
My project is very huge, and changing the string error: was not a good idea.
MSBuild parses the output of any command it executes for standardized error or warning messages. Having error : in the output of your bat will be detected by MSBuild as an actual error message and cause the build to be marked as failure.
You can see the error detection code of MSBuild here and a blog post from 2006 that describes this feature.
Now you have several options to solve this:
Change your project to use the Exec command to run the bat with the IgnoreStandardErrorWarningFormat property:
<Exec Command="path/to/your/batfile.bat" IgnoreStandardErrorWarningFormat="true"></Exec>
Change the text in your code:
LOGD("invalid data length: %d", datalen);
Fix the compiler warning that causes that line to be printed out in the first place.

Unresolved external symbol when linking ffmpeg/avcodec with Visual Studio 2015?

I'm trying to build ffmpeg/avcodec library with Visual Studio 2015 and it gives me following linking error:
allcodecs.obj : error LNK2001: unresolved external symbol ff_h263_vaapi_hwaccel
allcodecs.obj : error LNK2001: unresolved external symbol ff_h263_vdpau_hwaccel
allcodecs.obj : error LNK2001: unresolved external symbol ff_h263_videotoolbox_hwaccel
...
The problem comes from this macro https://github.com/FFmpeg/FFmpeg/blob/master/libavcodec/allcodecs.c#L34
#include "config.h"
#include "avcodec.h"
#include "version.h"
...
#define REGISTER_HWACCEL(X, x) \
{ \
extern AVHWAccel ff_##x##_hwaccel; \
if (CONFIG_##X##_HWACCEL) \
av_register_hwaccel(&ff_##x##_hwaccel); \
}
...
void avcodec_register_all(void)
{
static int initialized;
if (initialized)
return;
initialized = 1;
/* hardware accelerators */
REGISTER_HWACCEL(H263_VAAPI, h263_vaapi);
REGISTER_HWACCEL(H263_VIDEOTOOLBOX, h263_videotoolbox);
...
I don't understand why do these methods even get declared when my config has them set to 0
#define CONFIG_H263_VAAPI_HWACCEL 0
#define CONFIG_H263_VDPAU_HWACCEL 0
#define CONFIG_H263_VIDEOTOOLBOX_HWACCEL 0
...
Btw it builds OK when I use msys/make tooling.
You may be trying to link C code with C++ name mangling.
Try:
extern "C"
{
#include <avcodec.h>
#include <avformat.h>
}

std::vector of std::tuples leads to unknown size

I want to store three arbitrary ints inside a std::vector without defining a struct/class. So I went for std::tuple<>:
std::vector<std::tuple<unsigned int, unsigned int, unsigned int>
Using MS VS 2013, it leads to the following error:
>c:\program files (x86)\microsoft visual studio 12.0\vc\include\vector(1628): error C2036: 'std::tuple<unsigned int,unsigned int,unsigned int> *' : unknown size
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\vector(1622) : while compiling class template member function 'void std::vector<std::tuple<unsigned int,unsigned int,unsigned int>,std::allocator<_Ty>>::_Tidy(void)'
1> with
1> [
1> _Ty=std::tuple<unsigned int,unsigned int,unsigned int>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\vector(945) : see reference to function template instantiation 'void std::vector<std::tuple<unsigned int,unsigned int,unsigned int>,std::allocator<_Ty>>::_Tidy(void)' being compiled
1> with
1> [
1> _Ty=std::tuple<unsigned int,unsigned int,unsigned int>
1> ]
1> d:\projects\gl33\src\nvf.cpp(39) : see reference to class template instantiation 'std::vector<std::tuple<unsigned int,unsigned int,unsigned int>,std::allocator<_Ty>>' being compiled
1> with
1> [
1> _Ty=std::tuple<unsigned int,unsigned int,unsigned int>
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =======
Is this due to limitations in the MSVS2013 compiler? Or am I doing something wrong?
A class type is known (its name is known) but its size is unknown if the type has only been forward-declared, but not defined. E.g.
struct X;
sizeof(X) // error: X is incomplete
The size of a type is important for pointer arithmetic, which is another hint when looking at the compiler error (which mentions a pointer to tuple).
MSDN provides the following example for C2036:
struct A* pA;
int main() {
pA++; // C2036, size of A not known
((char*&)pA)++; // OK, if sizeof(A) == sizeof(char)
}
Where the struct A* pa implicitly forward-declares struct A.
Such a situation can happen with headers of the Standard Library when you don't include all required headers yourself. There are interdependencies between the types in the Standard Library. If a Standard Library header requires only a forward-declaration of tuple, it won't include the heavyweight tuple header itself in an effort to reduce compilation times.
I could reproduce the issue in the OP by including only <vector> but not <tuple>. Solution: manually include all headers you need types from - when using vector<tuple<..>>, include <tuple> (as well as <vector>). In general, including a header guarantees the availability of a certain set of types. To maximize portability, always make sure the headers you've included guarantee that you can use all the types in your program(*).
(*) More specifically, you should make sure that you have a definition for all types your program needs a definition for. Standard Library containers require their value types to be complete (at least at the point where the class template is instantiated). Hence, if your program requires a definition of vector<tuple<unsigned, unsigned>>, it also requires a definition of tuple<unsigned, unsigned>.

Build error when trying to run an OpenGL example in VC

What I can''t understand is why the both 2 individual .cpp files compile but the solution does not build. The code that won't compile even though I follow the instructions is
// Two-Dimensional Sierpinski Gasket
// Generated using randomly selected vertices and bisection
#include "Angel.h"
#include <GL/glut.h>
#include <GL/glew.h>
#pragma comment(lib, "glew32.lib")
const int NumPoints = 5000;
//----------------------------------------------------------------------------
void
init( void )
{
vec2 points[NumPoints];
// Specifiy the vertices for a triangle
vec2 vertices[3] = {
vec2( -1.0, -1.0 ), vec2( 0.0, 1.0 ), vec2( 1.0, -1.0 )
};
// Select an arbitrary initial point inside of the triangle
points[0] = vec2( 0.25, 0.50 );
// compute and store N-1 new points
for ( int i = 1; i < NumPoints; ++i ) {
int j = rand() % 3; // pick a vertex at random
// Compute the point halfway between the selected vertex
// and the previous point
points[i] = ( points[i - 1] + vertices[j] ) / 2.0;
}
// Create a vertex array object
GLuint vao;
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );
// Create and initialize a buffer object
GLuint buffer;
glGenBuffers( 1, &buffer );
glBindBuffer( GL_ARRAY_BUFFER, buffer );
glBufferData( GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW );
// Load shaders and use the resulting shader program
GLuint program = InitShader( "vshader21.glsl", "fshader21.glsl" );
glUseProgram( program );
// Initialize the vertex position attribute from the vertex shader
GLuint loc = glGetAttribLocation( program, "vPosition" );
glEnableVertexAttribArray( loc );
glVertexAttribPointer( loc, 2, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(0) );
glClearColor( 1.0, 1.0, 1.0, 1.0 ); // white background
}
//----------------------------------------------------------------------------
void
display( void )
{
glClear( GL_COLOR_BUFFER_BIT ); // clear the window
glDrawArrays( GL_POINTS, 0, NumPoints ); // draw the points
glFlush();
}
//----------------------------------------------------------------------------
void
keyboard( unsigned char key, int x, int y )
{
switch ( key ) {
case 033:
exit( EXIT_SUCCESS );
break;
}
}
//----------------------------------------------------------------------------
int
main( int argc, char **argv )
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_RGBA );
glutInitWindowSize( 512, 512 );
// If you are using freeglut, the next two lines will check if
// the code is truly 3.2. Otherwise, comment them out
glutInitContextVersion( 3, 1 );
glutInitContextProfile( GLUT_CORE_PROFILE );
glutCreateWindow( "Sierpinski Gasket" );
glewInit();
init();
glutDisplayFunc( display );
glutKeyboardFunc( keyboard );
glutMainLoop();
return 0;
}
I get this strange error message when I try to build the example project from Edward Angel's OpenGL site:
1>------ Build started: Project: 6E test, Configuration: Release Win32 ------
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewBindBuffer
1>example1.obj : error LNK2001: unresolved external symbol __imp____glutCreateWindowWithExit#8
1>example1.obj : error LNK2001: unresolved external symbol __imp____glutInitWithExit#12
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewVertexAttribPointer
1>example1.obj : error LNK2001: unresolved external symbol __imp__glewInit#0
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewGenVertexArrays
1>example1.obj : error LNK2001: unresolved external symbol __imp__glutInitContextProfile#4
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewUseProgram
1>example1.obj : error LNK2001: unresolved external symbol __imp__glutInitContextVersion#8
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewBufferData
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewBindVertexArray
1>example1.obj : error LNK2001: unresolved external symbol __imp__glutInitDisplayMode#4
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewGetAttribLocation
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewGenBuffers
1>example1.obj : error LNK2001: unresolved external symbol __imp__glutKeyboardFunc#4
1>example1.obj : error LNK2001: unresolved external symbol __imp__glutMainLoop#0
1>example1.obj : error LNK2001: unresolved external symbol __imp__glutInitWindowSize#8
1>example1.obj : error LNK2001: unresolved external symbol __imp__glutDisplayFunc#4
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewEnableVertexAttribArray
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewCreateShader
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewGetShaderInfoLog
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewLinkProgram
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewCompileShader
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewShaderSource
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewGetProgramiv
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewGetShaderiv
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewGetProgramInfoLog
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewCreateProgram
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewAttachShader
1>C:\Users\student\Downloads\6E_example1_VC10\6E test\Release\6E test.exe : fatal error LNK1120: 29 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
It looks to me like I've tried to link in incompatible libraries, what does the above look like to you? The complete source code I try to run (with Windows 7) is
// Two-Dimensional Sierpinski Gasket
// Generated using randomly selected vertices and bisection
#pragma comment(lib, "glew32.lib")
#include "Angel.h"
const int NumPoints = 5000;
//----------------------------------------------------------------------------
void
init( void )
{
vec2 points[NumPoints];
// Specifiy the vertices for a triangle
vec2 vertices[3] = {
vec2( -1.0, -1.0 ), vec2( 0.0, 1.0 ), vec2( 1.0, -1.0 )
};
// Select an arbitrary initial point inside of the triangle
points[0] = vec2( 0.25, 0.50 );
// compute and store N-1 new points
for ( int i = 1; i < NumPoints; ++i ) {
int j = rand() % 3; // pick a vertex at random
// Compute the point halfway between the selected vertex
// and the previous point
points[i] = ( points[i - 1] + vertices[j] ) / 2.0;
}
// Create a vertex array object
GLuint vao;
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );
// Create and initialize a buffer object
GLuint buffer;
glGenBuffers( 1, &buffer );
glBindBuffer( GL_ARRAY_BUFFER, buffer );
glBufferData( GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW );
// Load shaders and use the resulting shader program
GLuint program = InitShader( "vshader21.glsl", "fshader21.glsl" );
glUseProgram( program );
// Initialize the vertex position attribute from the vertex shader
GLuint loc = glGetAttribLocation( program, "vPosition" );
glEnableVertexAttribArray( loc );
glVertexAttribPointer( loc, 2, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(0) );
glClearColor( 1.0, 1.0, 1.0, 1.0 ); // white background
}
//----------------------------------------------------------------------------
void
display( void )
{
glClear( GL_COLOR_BUFFER_BIT ); // clear the window
glDrawArrays( GL_POINTS, 0, NumPoints ); // draw the points
glFlush();
}
//----------------------------------------------------------------------------
void
keyboard( unsigned char key, int x, int y )
{
switch ( key ) {
case 033:
exit( EXIT_SUCCESS );
break;
}
}
//----------------------------------------------------------------------------
int
main( int argc, char **argv )
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_RGBA );
glutInitWindowSize( 512, 512 );
// If you are using freeglut, the next two lines will check if
// the code is truly 3.2. Otherwise, comment them out
glutInitContextVersion( 3, 1 );
glutInitContextProfile( GLUT_CORE_PROFILE );
glutCreateWindow( "Sierpinski Gasket" );
glewInit();
init();
glutDisplayFunc( display );
glutKeyboardFunc( keyboard );
glutMainLoop();
return 0;
}
#pragma comment(lib, "glew32.lib")
#include "Angel.h"
namespace Angel {
// Create a NULL-terminated string by reading the provided file
static char*
readShaderSource(const char* shaderFile)
{
FILE* fp = fopen(shaderFile, "r");
if ( fp == NULL ) { return NULL; }
fseek(fp, 0L, SEEK_END);
long size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
char* buf = new char[size + 1];
fread(buf, 1, size, fp);
buf[size] = '\0';
fclose(fp);
return buf;
}
// Create a GLSL program object from vertex and fragment shader files
GLuint
InitShader(const char* vShaderFile, const char* fShaderFile)
{
struct Shader {
const char* filename;
GLenum type;
GLchar* source;
} shaders[2] = {
{ vShaderFile, GL_VERTEX_SHADER, NULL },
{ fShaderFile, GL_FRAGMENT_SHADER, NULL }
};
GLuint program = glCreateProgram();
for ( int i = 0; i < 2; ++i ) {
Shader& s = shaders[i];
s.source = readShaderSource( s.filename );
if ( shaders[i].source == NULL ) {
std::cerr << "Failed to read " << s.filename << std::endl;
exit( EXIT_FAILURE );
}
GLuint shader = glCreateShader( s.type );
glShaderSource( shader, 1, (const GLchar**) &s.source, NULL );
glCompileShader( shader );
GLint compiled;
glGetShaderiv( shader, GL_COMPILE_STATUS, &compiled );
if ( !compiled ) {
std::cerr << s.filename << " failed to compile:" << std::endl;
GLint logSize;
glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logSize );
char* logMsg = new char[logSize];
glGetShaderInfoLog( shader, logSize, NULL, logMsg );
std::cerr << logMsg << std::endl;
delete [] logMsg;
exit( EXIT_FAILURE );
}
delete [] s.source;
glAttachShader( program, shader );
}
/* link and error check */
glLinkProgram(program);
GLint linked;
glGetProgramiv( program, GL_LINK_STATUS, &linked );
if ( !linked ) {
std::cerr << "Shader program failed to link" << std::endl;
GLint logSize;
glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logSize);
char* logMsg = new char[logSize];
glGetProgramInfoLog( program, logSize, NULL, logMsg );
std::cerr << logMsg << std::endl;
delete [] logMsg;
exit( EXIT_FAILURE );
}
/* use program object */
glUseProgram(program);
return program;
}
} // Close namespace Angel block
Update
I changed the code to begin like below and it's still not building and I still get the build error.
#pragma comment(lib, "glew32.lib")
#define GLEW_STATIC 1
#include "Angel.h"
#include <GL\glew.h>
Update 150529
I can build the freeglut GLUT examples so GLUT appears correctly installed with VC, but then when I doubleclick one of the example builds it says that freeglut.dll is not installed on my system. I still get the same compilation error when trying to build Angel's example. Why? What am I doing wrong? What should I do?
1>------ Build started: Project: 6E test, Configuration: Release Win32 ------
1> example1.cpp
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\Angel.h(65): warning C4305: 'initializing' : truncation from 'double' to 'const GLfloat'
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(698): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(699): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(700): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(721): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(723): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(726): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(742): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1> InitShader.cpp
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\Angel.h(65): warning C4305: 'initializing' : truncation from 'double' to 'const GLfloat'
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(698): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(699): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(700): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(721): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(723): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(726): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(742): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>..\CODE\InitShader.cpp(13): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\stdio.h(218) : see declaration of 'fopen'
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewBindBuffer
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewVertexAttribPointer
1>example1.obj : error LNK2001: unresolved external symbol __imp__glewInit#0
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewGenVertexArrays
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewUseProgram
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewBufferData
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewBindVertexArray
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewGetAttribLocation
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewGenBuffers
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewEnableVertexAttribArray
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewCreateShader
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewGetShaderInfoLog
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewLinkProgram
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewCompileShader
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewShaderSource
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewGetProgramiv
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewGetShaderiv
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewGetProgramInfoLog
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewCreateProgram
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewAttachShader
1>C:\Users\student\Downloads\6E_example1_VC10\6E test\Release\6E test.exe : fatal error LNK1120: 20 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Update
I tried asking about this again and it still won't work:
Linker error with glew and Visual Studio on windows 7
You're missing GLEW (The OpenGL Extension Wrangler Library). You can link this in with the following pragma somewhere in your source code:
#pragma comment(lib, "glew32.lib")
Or you can modify the linker flags in the project settings. This assumes that you have the GLEW library installed. On my system, I installed it at the following path:
C:\Program Files (x86)/Microsoft Visual Studio 10.0/VC/lib/glew32.lib
The path may be different on your system, and there are other ways of linking with GLEW if you don't want to install it.
You can let go of GLEW. All it does is to get the GL (and WGL) extensions loaded. Nevertheless you can do it yourself:
1.include the headers (google the file names for download):
#include<GL/glext.h>
#include<GL/wglext.h>
2.declare the function pointers:
PFNGLGENVERTEXARRAYSPROC glGenVertexArrays;
PFNGLBINDVERTEXARRAYPROC glBindVertexArray;
PFNWGLMAKECONTEXTCURRENTARBPROC wglMakeContextCurrentARB;
//etc.
3.load the function pointers via wglGetProcAddress:
glGenVertexArrays = (PFNGLGENVERTEXARRAYSPROC)wglGetProcAddress("glGenVertexArrays");
glBindVertexArray = (PFNGLBINDVERTEXARRAYPROC)wglGetProcAddress("glBindVertexArray");
wglMakeContextCurrentARB = (PFNWGLMAKECONTEXTCURRENTARBPROC)wglGetProcAddress("wglMakeContextCurrentARB");
//etc
4.let the example code use them (no changes)!
glGenVertexArrays( 1, &vao );
//etc

Resources