C ++命名空间和链接无法正常工作

时间:2018-05-23 03:51:48

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

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

add_executable (POC eb_pms.cpp url_binder.cpp)

所以这个问题看起来很小,但是,我无法找到任何解决方案。或者,我应该说,没有一篇文章以简单的方式解释它,所以我能理解。我来自像C#/ Java这样的强类型背景。我正在尝试使用Simple Web ServerRapidJSON在C ++ 11中构建Rest API。由于我们正在构建此项目以成为更大解决方案的一部分,因此我无法使用命令行链接该程序。我必须使用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();
}

网址绑定器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 binder 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,这样我就可以使用url binder中我主cpp类中的方法了。我试图在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

我尝试研究已经有好几个小时了。但是我找不到那件丢失的东西。任何帮助将不胜感激。

0 个答案:

没有答案