# VTK cannot open file OpenGL::GL.lib

**URL:** https://discourse.vtk.org/t/vtk-cannot-open-file-opengl-gl-lib/9060
**Category:** Support
**Created:** [August 2, 2022, 6:26am UTC](https://discourse.vtk.org/t/vtk-cannot-open-file-opengl-gl-lib/9060 "2022-08-02T06:26:16Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [August 2, 2022, 6:26am UTC](https://discourse.vtk.org/t/vtk-cannot-open-file-opengl-gl-lib/9060/1 "2022-08-02T06:26:17Z")

</div>

I want to link `RenderingOpenGL2` in my project:

```auto
find_package(VTK
COMPONENTS
   ...
   RenderingOpenGL2
)

```

But the cmake reports a bug:

```auto
Target "Pro" links to target "OpenGL::GL" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing?

```

If I remove `RenderingOpenGL2` from `find_package`, the cmake is OK. But my project directly return 0 with no window is shown. My code is

```auto
	vtkSmartPointer<vtkSphereSource> sphere = vtkSmartPointer<vtkSphereSource>::New();
	sphere->Update();
	vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
	mapper->SetInputData(sphere->GetOutput());
	vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
	actor->SetMapper(mapper);
	vtkSmartPointer<vtkRenderer> render = vtkSmartPointer<vtkRenderer>::New();
	render->AddActor(actor);
	vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
	renWin->AddRenderer(render);
	renWin->Render();
	vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
	iren->SetRenderWindow(renWin);
	iren->Initialize();
	iren->Start();

```

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [August 3, 2022, 9:26pm UTC](https://discourse.vtk.org/t/vtk-cannot-open-file-opengl-gl-lib/9060/2 "2022-08-03T21:26:49Z")

</div>

Hello,

Why are you find\_package()-ing RenderingOpenGL2 in the configuration of your application? That configuration is done when building VTK.

regards,

Paulo

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [August 18, 2022, 5:48am UTC](https://discourse.vtk.org/t/vtk-cannot-open-file-opengl-gl-lib/9060/3 "2022-08-18T05:48:07Z")

</div>

@Paulo_Carvalho Thank you for your kindly reply. The reason why I include `RenderingOpenGL2` in `find_package` is that the vtk window don’t show any thing without `RenderingOpenGL2`.

My project folder is:

```auto
build
VTKVisualization
    CMakeLists.txt
    LineActor.h
    LineActor.cpp
    VTKVisualizationGlobal.h
    VTKVisualizationTest (folder)
        CMakeLists.txt
        main.cpp

```

The `VTKVisualization/CMakeLists.txt` is:

```auto

if (WIN32)
	set(PROJECTTYPE dll)
	set(SUBPROJECT_NAME CommonVTKVisualization)
endif (WIN32)

file(GLOB SOURCES "*.cpp" "*.c" "*.hpp")
file(GLOB HEADERS "*.h")

add_library(VTKVisualization SHARED ${SOURCES} ${HEADERS})

find_package(VTK 
	COMPONENTS
		FiltersSources
		FiltersGeneral
		RenderingCore
		InteractionStyle
		#RenderingOpenGL2
)

target_link_libraries(VTKVisualization 
	${VTK_LIBRARIES}
)

SET_TARGET_PROPERTIES(VTKVisualization
	PROPERTIES
	COMPILE_DEFINITIONS VTKVisualization_EXPORTS
)

add_subdirectory(VTKVisualizationTest)

```

The `LineActor.h` is:

```auto
#ifndef LINEACTOR_HEADER_H
#define LINEACTOR_HEADER_H

#include "VTKVisualizationGlobal.h"
#include "vtkLineSource.h"
#include "vtkFollower.h"
#include "vtkProperty.h"
#include "vtkPolyDataMapper.h"

class VTKVisualization_EXPORT LineActor : public vtkFollower
{
public:
	static LineActor* New();
	vtkTypeMacro(LineActor, vtkFollower);

	void SetPoint1(double x, double y, double z);
	void SetPoint2(double x, double y, double z);

protected:
	LineActor();

protected:
	vtkSmartPointer<vtkLineSource> m_lineSource_;
	vtkSmartPointer<vtkPolyDataMapper> m_mapper_;
};
typedef vtkSmartPointer<LineActor> LineActorPtr;
#endif

```

The `VTKVisualization/VTKVisualizationTest/CMakeLists.txt` is:

```auto

set(SUBPROJECT_NAME VTKVisualizationTest)

file(GLOB SOURCES "*.cpp" "*.c" "*.hpp")
file(GLOB HEADERS "*.h")

add_executable(VTKVisualizationTest ${SOURCES} ${HEADERS})

find_package(VTK 
	COMPONENTS
		CommonCore
		RenderingFreeType
		InteractionStyle
		#RenderingOpenGL2
)

message(${VTK_LIBRARIES})
target_link_libraries(VTKVisualizationTest 
	${VTK_LIBRARIES}
	VTKVisualization
)

vtk_module_autoinit(
TARGETS VTKVisualizationTest
MODULES ${VTK_LIBRARIES}
)

```

1. Include `RenderingOpenGL2` in both `VTKVisualization/CMakeLists.txt` and `VTKVisualization/VTKVisualizationTest/CMakeLists.txt`, the project can correctly display a line.
2. Do not include `RenderingOpenGL2` in both `VTKVisualization/CMakeLists.txt` and `VTKVisualization/VTKVisualizationTest/CMakeLists.txt`, the project directly return 0 with no vtk window is shown.
3. Remove `RendingOpenGL2` in `VTKVisualization/CMakeLists.txt`, while include `RendingOpenGL2` in `VTKVisualization/VTKVisualizationTest/CMakeLists.txt`, the bug is reported in CMake `Generate`:

```auto
CMake Error at VTKVisualization/VTKVisualizationTest/CMakeLists.txt:7 (add_executable):
  Target "VTKVisualizationTest" links to target "OpenGL::GL" but the target
  was not found. Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?

```

Any suggestion is appreciated~~~

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [August 24, 2022, 1:25pm UTC](https://discourse.vtk.org/t/vtk-cannot-open-file-opengl-gl-lib/9060/4 "2022-08-24T13:25:27Z")

</div>

This smells like a `FindOpenGL` getting the wrong GLVND selection. This should be fixed on `master`, but you may need to set `OpenGL_GL_PREFERENCE` to `LEGACY` manually for older releases.

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [August 29, 2022, 5:36am UTC](https://discourse.vtk.org/t/vtk-cannot-open-file-opengl-gl-lib/9060/5 "2022-08-29T05:36:38Z")

</div>

> but you may need to set `OpenGL_GL_PREFERENCE` to `LEGACY` manually for older releases.

How to set it? Set it in CMakeLists.txt?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [August 29, 2022, 1:08pm UTC](https://discourse.vtk.org/t/vtk-cannot-open-file-opengl-gl-lib/9060/6 "2022-08-29T13:08:43Z")

</div>

Yes, that would work.

```nohighlight
set(OpenGL_GL_PREFERENCE LEGACY)
find_package(VTK …)

```

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [August 30, 2022, 6:26am UTC](https://discourse.vtk.org/t/vtk-cannot-open-file-opengl-gl-lib/9060/7 "2022-08-30T06:26:18Z")

</div>

@ben.boeckel I have tried it, but it do not work.

My vtk is 9.1.0.
