C++命名空间相关链接错误排查及修复方案
Let's walk through what's happening and how to fix that frustrating linker error:
Background
I have a strong background in statically typed languages like C# and Java, and I'm currently working on a REST API using C++11, paired with Simple Web Server and RapidJSON. The project relies entirely on CMakeLists.txt for building (no manual command-line linking), and I have two key source files:
eb_pms.cpp: The main entry point for the projecturl_binder.cpp: Contains theEBPMSnamespace and itsURLBinderclass with the methods I need to call
The Issue
When I try to invoke methods from EBPMS::URLBinder in eb_pms.cpp, running make results in an Undefined symbols for architecture x86_64 error. This is a classic linker problem—meaning the linker can't find the compiled definitions for the URLBinder methods I'm using.
The Fix
The root cause is simple: my original CMakeLists.txt wasn't including url_binder.cpp in the executable build target. Without adding it, CMake never compiles that file, so the linker has no access to the method definitions.
Update your add_executable line in CMakeLists.txt to include both source files:
add_executable (POC eb_pms.cpp url_binder.cpp)
After making this change, re-run cmake and make—the linker error should be gone, and your code will successfully link against the URLBinder implementation.
内容的提问来源于stack exchange,提问作者Oo-_-oO

