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.

0
3 comments

Hello!

All the files are in the same directory, yet when i press run, i get linker error

  1. Could you please clarify where you press Run, in the gutter or on the toolbar?
  2. What run/debug configuration is selected in the switcher on the toolbar? Is it Maze1 or main.cpp?

0

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

0

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.cpp file (see https://www.jetbrains.com/help/clion/run-single-file.html for more info), and not your Maze1 target.

Please select the Maze1 configuration in the drop-down on the toolbar and click the Build button. It should work.

0

Please sign in to leave a comment.