You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CLion中出现Undefined Reference(未定义引用)问题求助

Fixing the Undefined Reference Error in Your CLion C++ Project

Hey, I recognize that undefined reference error—let's get this sorted out quickly!

The root problem here is your CMakeLists.txt: right now, you're only telling CMake to compile main.cpp into your executable, but you're missing testclass.cpp where the actual implementation of testclass::foo() lives. The linker can't find the code for that method because it was never compiled in the first place.

Here's the simple fix: update your CMakeLists.txt to include both source files in the add_executable command:

cmake_minimum_required(VERSION 3.9)
project(untitled1)
set(CMAKE_CXX_STANDARD 11)
add_executable(untitled1 main.cpp testclass.cpp)

Once you save this change, go back to CLion, reload the CMake project (you'll see a small prompt in the top right corner to do this), and rebuild. The linker will now have access to the foo() implementation from testclass.cpp, so your error should disappear, and your program should run to print "Hello, World!" as expected.

内容的提问来源于stack exchange,提问作者Yhprums

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:23:45