Unable to run vtk example in clion

I installed vtk on my Mac using
brew install vtk

then for testing purpose, I download a random example https://examples.vtk.org/site/Cxx/GeometricObjects/Cube/
then using the instruction as mentioned in the page to run example, it ran successfully.

Then when I opened the project using Clion, I encountered build error.

[1/1] Linking CXX executable Cube.app/Contents/MacOS/Cube
FAILED: Cube.app/Contents/MacOS/Cube 
: && /Library/Developer/CommandLineTools/usr/bin/c++ -O3 -DNDEBUG -arch x86_64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  @CMakeFiles/Cube.rsp -o Cube.app/Contents/MacOS/Cube  && :
ld: warning: ignoring file /opt/homebrew/lib/libvtkRenderingFreeType-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libvtkRenderingOpenGL2-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libvtkRenderingContext2D-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libvtkRenderingHyperTreeGrid-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libvtkRenderingUI-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libvtkRenderingContextOpenGL2-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libvtkInteractionStyle-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libvtkRenderingCore-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libvtkCommonColor-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libvtkCommonExecutionModel-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libvtkCommonTransforms-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libvtkRenderingGL2PSOpenGL2-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libvtkFiltersGeneral-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libvtkFiltersCore-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libvtkCommonDataModel-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libvtkCommonMisc-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libvtksys-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libvtkfreetype-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libvtkkissfft-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libvtkCommonMath-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libGLEW.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/lib/libvtkCommonCore-9.2.9.2.6.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
Undefined symbols for architecture x86_64:
  "vtkInteractionStyle_AutoInit_Construct()", referenced from:
      __GLOBAL__sub_I_Cube.cxx in Cube.cxx.o
  "vtkRenderingOpenGL2_AutoInit_Construct()", referenced from:
      __GLOBAL__sub_I_Cube.cxx in Cube.cxx.o
  "vtkRenderingFreeType_AutoInit_Construct()", referenced from:
      __GLOBAL__sub_I_Cube.cxx in Cube.cxx.o
  "vtkDataSet::GetScalarRange()", referenced from:
      _main in Cube.cxx.o
  "vtkPolyData::New()", referenced from:
      _main in Cube.cxx.o
  "vtkPolyData::SetPolys(vtkCellArray*)", referenced from:
      _main in Cube.cxx.o
  "vtkRenderer::SetActiveCamera(vtkCamera*)", referenced from:
      _main in Cube.cxx.o
  "vtkRenderer::New()", referenced from:
      _main in Cube.cxx.o
  "vtkRenderer::AddActor(vtkProp*)", referenced from:
      _main in Cube.cxx.o
  "vtkCellArray::New()", referenced from:
      _main in Cube.cxx.o
  "vtkDataArray::InsertTuple1(long long, double)", referenced from:
      _main in Cube.cxx.o
  "vtkFloatArray::New()", referenced from:
      _main in Cube.cxx.o
  "vtkNamedColors::GetColor3d(vtkStdString const&)", referenced from:
      _main in Cube.cxx.o
  "vtkNamedColors::New()", referenced from:
      _main in Cube.cxx.o
  "vtkRenderWindow::New()", referenced from:
      _main in Cube.cxx.o
  "vtkPolyDataMapper::SetInputData(vtkPolyData*)", referenced from:
      _main in Cube.cxx.o
  "vtkPolyDataMapper::New()", referenced from:
      _main in Cube.cxx.o
  "vtkDataSetAttributes::SetScalars(vtkDataArray*)", referenced from:
      _main in Cube.cxx.o
  "vtkDebugLeaksManager::vtkDebugLeaksManager()", referenced from:
      __GLOBAL__sub_I_Cube.cxx in Cube.cxx.o
  "vtkDebugLeaksManager::~vtkDebugLeaksManager()", referenced from:
      __GLOBAL__sub_I_Cube.cxx in Cube.cxx.o
  "vtkRenderWindowInteractor::SetRenderWindow(vtkRenderWindow*)", referenced from:
      _main in Cube.cxx.o
  "vtkRenderWindowInteractor::New()", referenced from:
      _main in Cube.cxx.o
  "vtkObjectFactoryRegistryCleanup::vtkObjectFactoryRegistryCleanup()", referenced from:
      __GLOBAL__sub_I_Cube.cxx in Cube.cxx.o
  "vtkObjectFactoryRegistryCleanup::~vtkObjectFactoryRegistryCleanup()", referenced from:
      __GLOBAL__sub_I_Cube.cxx in Cube.cxx.o
  "vtksys::SystemToolsManager::SystemToolsManager()", referenced from:
      __GLOBAL__sub_I_Cube.cxx in Cube.cxx.o
  "vtksys::SystemToolsManager::~SystemToolsManager()", referenced from:
      __GLOBAL__sub_I_Cube.cxx in Cube.cxx.o
  "vtkActor::New()", referenced from:
      _main in Cube.cxx.o
  "vtkCamera::SetPosition(double, double, double)", referenced from:
      _main in Cube.cxx.o
  "vtkCamera::SetFocalPoint(double, double, double)", referenced from:
      _main in Cube.cxx.o
  "vtkCamera::New()", referenced from:
      _main in Cube.cxx.o
  "vtkPoints::New()", referenced from:
      _main in Cube.cxx.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

I don’t understand what went wrong.

ld: symbol(s) not found for architecture x86_64

this is a very general error and could be caused by a lot of reasons.
I’ve searched through and didn’t find anyone who has the same problem.

My configuration:
macOS 13.4.1 (22F82)

vtk info:
image

Any help is appreciated!

after some desperate searches I’ve found the solution. The error occurs because Mac M2 chip uses arm64 architecture while clion tries to build using x86. So the solution is to add

if (APPLE)
    SET(CMAKE_OSX_ARCHITECTURES arm64)
endif ()

these 3 lines into CMakeLists.txt
and now it builds without any error.

for anyone who needs reference, my Mac is Mac mini m2(2023)

1 Like

Note that x86_64 processes will only launch the Intel “half” of universal binaries, so if CLion is not arm64-enabled yet, CMake and ninja will execute as Intel binaries and the compilers they launch will see an “Intel” machine as being native.

thanks for pointing out!
Also when downloading clion, there’s an option to download intel or m1 version.
It would be nice if there’s a way to differenciate between x86_64 architecture(Intel chip) and arm64(M1/M2) in CMake.
There’s actually a discussion about this in CMake forum: https://discourse.cmake.org/t/how-to-determine-which-architectures-are-available-apple-m1/2401/9