CLion中出现Undefined Reference(未定义引用)问题求助
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

