CLion 运行报错解决
代码正确,但是一直提示错误 undefined reference to `PrintHandler::PrintHandler(Looper*)' collect2.exe: error: ld returned 1 exit status 无意中发现自己运行的是 main.cpp 而不是整个项目,运行整个项目就会正常了。 具体原因见 AI 这是一个 CLion 或 CMake 配置相关的问题,不是代码逻辑问题,而是你在运行 main.cpp 文件单独作为“单文件运行”时,编译器没有正确链接整个项目的所有 .cpp 文件(例如 Looper.cpp、PrintHandler.cpp 等),从而导致: undefined reference to `Looper::postMessage(...)` undefined reference to `vtable for PrintHandler` ... ✅ 原因详解 在 CLion 中点击右键运行 main.cpp 文件 时: 它会临时生成一个只编译 main.cpp 的目标,没有链接你项目中定义的其他源文件(.cpp),所以报链接错误。 而你点击的是: ❌ 右键 main.cpp → Run main.cpp 只运行单文件,不会编译整个项目。 ✅ 正确做法 请始终使用: ✅ Run 'EmployeeSystem' 或 Run '<your_target_name>' 也就是你在 CMakeLists.txt 中通过 add_executable() 定义的那个目标,例如: ...