CLion Cmake linker error
Answered
I have a fairly simple project consisting of 3 files.
Main.cpp - The main executable file
Maze.h - Declaration of a single class (Maze) with it's methods and variables
Maze.cpp - Definitions for the Maze class.
I am using Clion 2025.2
All the files are in the same directory, yet when i press run, i get linker error:
/usr/bin/ld: /tmp/cczGKiNx.o: in function `main':
main.cpp:(.text+0x73): undefined reference to `Maze::Maze(int, int)' I have determined that is is indeed a cmake error, because when I compile it manually using g++ it works flawlessly
Here's the CMakeLists.txt file:
cmake_minimum_required(VERSION 4.0)
project(Maze)
set(CMAKE_CXX_STANDARD 20)
add_executable(Maze1
main.cpp
Maze.cpp
)And here's the Makefile that works with no problems.
CXX = g++
CXXFLAGS = -std=c++20 -Wall -Wextra -Wpedantic -g
TARGET = Maze1
SRC = main.cpp Maze.cpp
OBJ = $(SRC:.cpp=.o)
all: $(TARGET)
$(TARGET): $(OBJ)
$(CXX) $(OBJ) -o $(TARGET)
%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
clean:
rm -f $(OBJ) $(TARGET)Could anybody tell me what causes the problem?
Thanks in advance for any help.
Please sign in to leave a comment.
Hello!
What run/debug configuration is selected in the switcher on the toolbar? Is it
Maze1ormain.cpp?I press it on the toolbar or just shift-F10
This is the screen of the toolbar.
As per the file that is being run it is indeed (as visible on the image) maze.cpp
This means that you're building a single
main.cppfile (see https://www.jetbrains.com/help/clion/run-single-file.html for more info), and not yourMaze1target.Please select the
Maze1configuration in the drop-down on the toolbar and click the Build button. It should work.