Hey everyone. Recently I've been itching to code in c++ along with openGL. the only problem is, I'm still a newbie and don't know how to do this. Everything I've looked up online fits into one of the following two categories-
1. I can't get it working.
2. It uses visual studio which I kind of despise. (I want to actually understand how to link .h and library's not just press a button in visual studio)

anyways I downloaded an openGL library off of this website. (I'm assuming that's the right website.) but I just don't have the slightest clue of what to do!

I haven't worked in C++ much but from what I gather, I need a makefile. I've looked things up but can't find much info. It may be just as simple as the fact that idk how my folders are supposed to be structured for it to work!

so if anyone would help me out or point me to a good recourse that would be much appreciated!

PS: Here's the github repo of the project- even though literally nothing has been done (except copying and pasting template code off of the opengl documentation)
Given you allude that visual studio is an option but not preferred, I assume this is windows.

Makefiles are not commonly used on non-unixy platforms, and especially so for software that's supposed to be portable across systems. Everybody has an opinion on what build systems are most sensible and least horrible, but I tend to reach for CMake so that's my suggestion.

You do still need a compiler, though. On Windows I'd still use the Microsoft compiler (or Clang, but not MinGW) which you can use without Visual Studio if you want to. The Build Tools For Visual Studio includes only the compiler, linker and such if you don't want the whole IDE.

GLFW has documentation for building with cmake so that shouldn't be too hard to follow. But having at it myself by cloning your repository and trying to build it myself, I wrote this CMakeLists.txt:

Code:
cmake_minimum_required(VERSION 3.0)

project(OpenGLTest)

include_directories(BEFORE ${CMAKE_SOURCE_DIR}/include)

add_executable(OpenGLTest src/main.cpp)
target_link_libraries(OpenGLTest ${CMAKE_SOURCE_DIR}/libs/glfw3.lib)


Then I opened a Visual Studio tools command prompt (which is a shortcut the tools install that gives you a shell configured with the appropriate tools) and invoked cmake to configure and build the program:

Code:
> cd OpenGLTest
> mkdir build
> cd build
> cmake ..
-- Building for: NMake Makefiles
-- The C compiler identification is MSVC 19.14.26431.0
-- The CXX compiler identification is MSVC 19.14.26431.0
-- Check for working C compiler: G:/Visual Studio 2017/VC/Tools/MSVC/14.14.26428/bin/Hostx64/x86/cl.exe
-- Check for working C compiler: G:/Visual Studio 2017/VC/Tools/MSVC/14.14.26428/bin/Hostx64/x86/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: G:/Visual Studio 2017/VC/Tools/MSVC/14.14.26428/bin/Hostx64/x86/cl.exe
-- Check for working CXX compiler: G:/Visual Studio 2017/VC/Tools/MSVC/14.14.26428/bin/Hostx64/x86/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/.../OpenGLTest/build
> cmake --build .
Scanning dependencies of target OpenGLTest
[ 50%] Building CXX object CMakeFiles/OpenGLTest.dir/src/main.cpp.obj
main.cpp
[100%] Linking CXX executable OpenGLTest.exe
LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
main.cpp.obj : error LNK2019: unresolved external symbol __imp__glClear@4 referenced in function _main
OpenGLTest.exe : fatal error LNK1120: 1 unresolved externals
LINK Pass 1 failed. with 1120
NMAKE : fatal error U1077: '"C:\Program Files\CMake\bin\cmake.exe"' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"G:\Visual Studio 2017\VC\Tools\MSVC\14.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"G:\Visual Studio 2017\VC\Tools\MSVC\14.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.

You're using an openGL function (glClear) that isn't provided by glfw so you'll also need to link an openGL library, but if I comment out that line and recompile I get a binary build/OpenGLTest.exe that creates a window as expected:
Tari wrote:
Given you allude that visual studio is an option but not preferred, I assume this is windows.

Makefiles are not commonly used on non-unixy platforms, and especially so for software that's supposed to be portable across systems. Everybody has an opinion on what build systems are most sensible and least horrible, but I tend to reach for CMake so that's my suggestion....


thanks for the help, i managed to get a long ways. i have a couple questions, as my directory did not end up looking like yours XD. So the first problem is that idk where to put the cmake list file. I put it in my src folder, but then the cmake wouldnt generate anything. i put it in the overarching directory openGLTest, and that got cmake to generate something, but i dont think its in the right place. the other thing is, I downloaded the binaries, should i download the source for it? your link to the opengl tutorial was for the source libraries, not the pre-compiled binaries.

here's a picture of whats happening. any help is much appreciated!

And I'm sorry this is probably really obvious what I'm doing wrong... forgive a newbie!

What you've got is reasonable- the difference in what got put in your build directory is related to cmake using a different generator.

The issue appears to be that you're building a 64-bit binary, when your glfw3.lib is 32-bit. That causes the unresolved externals, since the linker ignores the 32-bit library (with the LNK4272 warning). Build a 32-bit binary or get a 64-bit library and it should work.
Tari wrote:
What you've got is reasonable- the difference in what got put in your build directory is related to cmake using a different generator.

The issue appears to be that you're building a 64-bit binary, when your glfw3.lib is 32-bit. That causes the unresolved externals, since the linker ignores the 32-bit library (with the LNK4272 warning). Build a 32-bit binary or get a 64-bit library and it should work.



Wow I got it to work! thx so much for your help!
Jcsq6 wrote:
the other thing is, I downloaded the binaries, should i download the source for it? your link to the opengl tutorial was for the source libraries, not the pre-compiled binaries.
I didn't notice this question before- using the source would prevent issues like you had regarding different (32 vs 64-bit) targets, though initial build would be a little slower.

Personally I would use the library sources, since then you don't need to check in (or if not checking in, otherwise acquire) the glfw binaries.
Tari wrote:
Jcsq6 wrote:
the other thing is, I downloaded the binaries, should i download the source for it? your link to the opengl tutorial was for the source libraries, not the pre-compiled binaries.
I didn't notice this question before- using the source would prevent issues like you had regarding different (32 vs 64-bit) targets, though initial build would be a little slower.

Personally I would use the library sources, since then you don't need to check in (or if not checking in, otherwise acquire) the glfw binaries.


ok sounds good! I'll try that out, see if I can get it working... (I long for the future days where hopefully I understand how things work, and it's not a matter of "getting it working" XD)
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement