C++ 命名空间和链接不起作用

标签 c++ c++11 namespaces linker-errors

更新:已修复。非常简单的解决方案。我只需要添加我的 Add_Executable 方法。这里:

add_executable (POC eb_pms.cpp url_binder.cpp)

所以这个问题看起来很小,但是,我找不到任何解决方案。或者,我应该说,没有一篇文章以简单的方式解释它,以便我能够理解。我来自像 C#/Java 这样的强类型背景。我正在尝试使用 Simple Web Server 在 C++11 中构建 Rest API和 RapidJSON .由于我们将此项目构建为更大解决方案的一部分,因此我无法使用命令行链接程序。我必须使用 CMakeLists 来编译和运行程序。这意味着,链接必须通过 CMakeLists.txt 文件完成。完整的源代码可以在这里找到 GitHub .我的环境是Macbook Pro + VS Code。所以我这里有两个 CPP:eb_pms.cpp、url_binder.cpp。 urlbinder 有一个命名空间 'EBPMS' 而 eb_pms 没有。以下是 ep_pms 的代码:

(头文件)

#include "server_http.hpp"
#include "client_http.hpp" //client for testing the API code
#include <boost/property_tree/ptree.hpp> /*for the shared pointer*/
#include "url_binder.h"
using HttpServer = SimpleWeb::Server<SimpleWeb::HTTP>;
using HttpClient = SimpleWeb::Client<SimpleWeb::HTTP>;
using Binder = EBPMS::URLBinder;
void bindUrls(HttpServer *server);

cpp文件:

# include "eb_pms.h"
using namespace boost::property_tree;
using namespace std;
using namespace EBPMS;
int main(){
    HttpServer server;
    server.config.port = 8081;
    bindUrls(&server);
    thread server_thread([&server]() {
        server.start();
  });
  server_thread.join();
}
void bindUrls(HttpServer *server){
    //URLBinder c;
    Binder binder; 
    binder.bindURL_string();
}

url 绑定(bind) h:

#include <boost/property_tree/ptree.hpp> /*for the shared pointer*/
#include "server_http.hpp"
#include "client_http.hpp" 
#include <iostream>
namespace EBPMS{
    class URLBinder{
        public: URLBinder();
        void bindURL_string();
    }; 
} 

url绑定(bind)cpp;

#include "url_binder.h"
using namespace boost::property_tree;
using namespace std;

namespace EBPMS{
    URLBinder::URLBinder() {}
    void URLBinder::bindURL_string(){ //HttpServer *server
        std::cout << "Trying to log if it comes" << std::endl;
    }
}

所以基本上我需要的只是找到一种方法将此文件与 NameSpace 与我的原始 cpp 链接起来,这样我就可以在我的主 cpp 类中使用 url binder 中的方法。我正在尝试调用 eb_pms.cpp 类中的函数。当我运行 make 命令时,出现此错误:

Scanning dependencies of target POC
[  2%] Building CXX object CMakeFiles/POC.dir/eb_pms.cpp.o
[  5%] Linking CXX executable POC
Undefined symbols for architecture x86_64:
"EBPMS::URLBinder::bindURL_string()", referenced from: _main in eb_pms.cpp.o
      bindUrls(SimpleWeb::Server<boost::asio::basic_stream_socket<boost::asio::ip::tcp>> >*) in eb_pms.cpp.o
"EBPMS::URLBinder::URLBinder()", referenced from: _main in eb_pms.cpp.o
  bindUrls(SimpleWeb::Server<boost::asio::basic_stream_socket<boost::asio::ip::tcp>> >*) in eb_pms.cpp.o
ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [POC] Error 1
make[1]: ***
[CMakeFiles/POC.dir/all] Error 2
make: *** [all] Error 2

我已经好几个小时没开始研究了。但是我找不到那 block 丢失的东西。任何帮助将不胜感激。

最佳答案

我只是猜测,因为你没有显示你的 CMakeLists.txt 文件...

我会说你没有将你的 url_binder.cpp 文件列为 CMake 中的源 add_executable命令。

必须列出您希望成为程序一部分的所有源文件。

关于C++ 命名空间和链接不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50479531/

相关文章:

C++:指针值更改退出函数

c++ - std::minmax initializer_list<T> 参数

函数执行器中的 C++ 歧义,参数在 vector 中传递

xml - 使用 xml :space without namespace declaration

c++ - 在只读场景中何时需要以空字符结尾的字符串?

c++ - 在 C++ 中以优雅的方式计时

c++ - 用于将多态基类的子类数组传递到构造函数中的最简单语法

c++ - analogRead 在库中返回 0

azure - 订阅未注册以使用命名空间 'Microsoft.Network'

r - 什么时候不应该使用 assignInNamespace?