VTK cannot open file OpenGL::GL.lib

I want to link RenderingOpenGL2 in my project:

find_package(VTK
COMPONENTS
   ...
   RenderingOpenGL2
)

But the cmake reports a bug:

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

	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();

Hello,

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

regards,

Paulo

@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:

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

The VTKVisualization/CMakeLists.txt is:


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:

#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:


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:
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~~~

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.

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

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

Yes, that would work.

set(OpenGL_GL_PREFERENCE LEGACY)
find_package(VTK …)

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

My vtk is 9.1.0.