OpenGL libraries with Clang compiler

I'm trying to allow the Clang C++ compiler to use OpenGL libraries on Windows 8.1 but can't get it to work. I created a test file that simply includes the library but does nothing else:

#include <windows.h>
#include <GL/glew.h>

The result from Clang is:

LINK : fatal error LNK1561: entry point must be defined
clang.exe: error: linker command failed with exit code 1561 (use -v to see invoc
ation)

I was first trying to use FreeGlut but then I found this thread http://www.cplusplus.com/forum/beginner/152942/ that said to use Glew. I downloaded Glew and copied the following files:

bin/glew32.dll to C:/windows/system32
lib/glew32.lib to D:/Programs/LLVM/lib/clang/4.0.0/lib/windows
include/GL/glew.h to D:/Programs/LLVM/lib/clang/4.0.0/include/GL
include/GL/wglew.h to D:/Programs/LLVM/lib/clang/4.0.0/include/GL

The instructions for Glew say to copy these files http://glew.sourceforge.net/install.html . Trying Glew still didn't fix the problem. Wikipedia says Glew is meant to help query and load OpenGL extensions so I'm not sure if it what I want anyway.

A problem I had with FreeGlut is the instructions are based on Microsoft Visual Studio. After using CMake the file freeglut.sln is created but it opens with Visual Studio and my 30 day trial period has long expired. A lot of instructions I find for using FreeGlut on Windows with Google searches are based on Microsoft Visual Studio. Is it even possible to get OpenGL to work for any compiler on Windows other than Microsoft Visual Studio?

I read about libraries needing to be linked before being used but when I searched specifically how to do that, I got a forum post where someone asked that question https://stackoverflow.com/questions/9516134/how-to-link-opengl-libraries-to-project-in-netbeans-7-1 and the response was to copy .h files into the include/GL directory, which is what I did with the FreeGlut header files.

Before I tried to get OpenGL to work with C++ I tried both Java and Python but couldn't get it to work for those langauges either. The problem with Python seemed to be that the OpenGLContext library has not been updated for Python3. Someone mentioned trying to use 2to3 converters but still couldn't get it to work.

1
2
3
4
5
#include <windows.h>
#include <GL/glew.h>

// Add a main function
int main() {}

And in the linker command line, you need to link with -lgl -lGLEW or the equivalents to actually use functions defined in libgl and libGLEW.

After using CMake the file freeglut.sln is created
Tell cmake to generate Unix makefiles (or whatever you need) when you invoke it:
cmake -G "Unix Makefiles" .
https://cmake.org/cmake/help/v3.0/manual/cmake-generators.7.html

Edit:
make sure you have set the default system compiler by exporting the environment variables CC, CXX before running cmake.
Last edited on
I set the variables in CMakeLists.txt:

1
2
3
4
5
export(CC="D:\Programs\LLVM\bin\clang")
export(CXX="D:\Programs\LLVM\bin\clang" cmake -G "Clang" "D:\downloads\freeglut-3.0.0.tar\freeglut-3.0.0\freeglut-3.0.0")
set(CMAKE_MAKE_PROGRAM "Clang")
set(CMAKE_C_COMPILER "Clang")
set(CMAKE_CXX_COMPILER "Clang")


But I still get an error message:

D:\downloads\freeglut-3.0.0.tar\freeglut-3.0.0\freeglut-3.0.0>cmake -G "Unix Mak
efiles"
CMake Error: CMake was unable to find a build program corresponding to "Unix Mak
efiles".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a differen
t build tool.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!


I also don't know which generator would be compatible with Clang on Windows. I tried the same command using "MSYS Makefiles" and "MinGW Makefiles" but I got the same error message. I tried the following on the command line:

1
2
D:\downloads\freeglut-3.0.0.tar\freeglut-3.0.0\freeglut-3.0.0>cmake -G "Clang"
CMake Error: Could not create named generator Clang


But it said it could not create a generator named Clang, even though I included

export(CXX="D:\Programs\LLVM\bin\clang" cmake -G "Clang" "D:\downloads\freeglut-3.0.0.tar\freeglut-3.0.0\freeglut-3.0.0")

in CMakeLists.txt. This page https://cmake.org/Wiki/CMake_FAQ#How_do_I_use_a_different_compiler.3F gives me the impression that the above export command is how to set it to use a different compiler.
You should not need to modify CMakeLists.txt. This is for the package developers.
Instead, you should set environment variables. If this is still a pain on Windows, you can use the -D option.

The generator is responsible for creating a script for a build system (Unix make, etc.) which has nothing to do with the compiler. Besides whatever build system comes with Visual Studio, I have no idea what build system(s) you have installed, but the name of it surely isn't Clang. I believe nmake still comes with Visual Studio -- try that?

The name of the C++ compiler is exactly clang++.
The name of the C compiler is exactly clang (assuming they are in your %PATH%)

This suggests a command line which appears something like this:
cmake . -G"NMake Makefiles" -DCXX=clang++ -DCC=clang
Last edited on
Thanks for the help but I still cannot make it work. I tried:

1
2
3
D:\downloads\freeglut-3.0.0.tar\freeglut-3.0.0\freeglut-3.0.0>cmake . -G"NMake M
akefiles" -DCXX=D:\Programs\LLVM\bin\clang++.exe -DCC=D:\Programs\LLVM\bin\clang
.exe


It would not find the files unless I specified the full path for clang.exe, even though I have D:\Programs\LLVM\bin in my PATH. The output was:


-- The C compiler identification is unknown
-- The CXX compiler identification is Clang 4.0.0
-- Check for working C compiler: D:/Programs/LLVM/bin/clang.exe
-- Check for working C compiler: D:/Programs/LLVM/bin/clang.exe -- broken
CMake Error at D:/Programs/CMake/share/cmake-3.5/Modules/CMakeTestCCompiler.cmak
e:61 (message):
  The C compiler "D:/Programs/LLVM/bin/clang.exe" is not able to compile a
  simple test program.

  It fails with the following output:

   Change Dir: D:/downloads/freeglut-3.0.0.tar/freeglut-3.0.0/freeglut-3.0.0/CMa
keFiles/CMakeTmp



  Run Build Command:"nmake" "/NOLOGO" "cmTC_d6793\fast"

        D:\Programs\MicrosoftVisualStudio\VC\bin\nmake.exe -f
  CMakeFiles\cmTC_d6793.dir\build.make /nologo -L
  CMakeFiles\cmTC_d6793.dir\build

  Building C object CMakeFiles/cmTC_d6793.dir/testCCompiler.c.obj

        D:\Programs\LLVM\bin\clang.exe /DWIN32 /D_WINDOWS /W3 -o
  CMakeFiles\cmTC_d6793.dir\testCCompiler.c.obj -c
  D:\downloads\freeglut-3.0.0.tar\freeglut-3.0.0\freeglut-3.0.0\CMakeFiles\CMake
Tmp\testCCompiler.c


  clang.exe: error: no such file or directory: '/DWIN32'

  clang.exe: error: no such file or directory: '/D_WINDOWS'

  clang.exe: error: no such file or directory: '/W3'

  NMAKE : fatal error U1077: 'D:\Programs\LLVM\bin\clang.exe' : return code
  '0x1'

  Stop.

  NMAKE : fatal error U1077:
  'D:\Programs\MicrosoftVisualStudio\VC\bin\nmake.exe' : return code '0x2'

  Stop.





  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:2 (PROJECT)


-- Configuring incomplete, errors occurred!
See also "D:/downloads/freeglut-3.0.0.tar/freeglut-3.0.0/freeglut-3.0.0/CMakeFil
es/CMakeOutput.log".
See also "D:/downloads/freeglut-3.0.0.tar/freeglut-3.0.0/freeglut-3.0.0/CMakeFil
es/CMakeError.log".


I then tried using clang-cl.exe for both the CC and CXX variables because I found this source https://stackoverflow.com/questions/34674753/why-doesnt-llvm-config-on-windows-emit-the-correct-parameters-for-clang-exe that says to use clang-cl.exe on Windows. I got the same output.

I found this source https://stackoverflow.com/questions/38171878/how-do-i-tell-cmake-to-use-clang-on-windows that says to include -T"LLVM-vs2014" in the command line but it still won't work (I also tried -T"LLVM-vs2017"). The output said the generator does not support the toolset specification:


D:\downloads\freeglut-3.0.0.tar\freeglut-3.0.0\freeglut-3.0.0>cmake . -G"NMake M
akefiles" -T"LLVM-vs2014" -DCXX=D:\Programs\LLVM\bin\clang-cl.exe -DCC=D:\Progra
ms\LLVM\bin\clang-cl.exe
CMake Error at CMakeLists.txt:2 (PROJECT):
  Generator

    NMake Makefiles

  does not support toolset specification, but toolset

    LLVM-vs2014

  was specified.


CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
See also "D:/downloads/freeglut-3.0.0.tar/freeglut-3.0.0/freeglut-3.0.0/CMakeFil
es/CMakeOutput.log".
See also "D:/downloads/freeglut-3.0.0.tar/freeglut-3.0.0/freeglut-3.0.0/CMakeFil
es/CMakeError.log".


I tried using -DCMAKE_MAKE_PROGRAM to specify the location of nmake.exe (based on this source https://stackoverflow.com/questions/38106848/what-is-the-d-define-to-tell-cmake-where-to-find-nmake ) and got the same results as above.
Topic archived. No new replies allowed.