[英]C++ Namespace and Linking not working
更新:已修復。 很簡單的解決方案 我只需添加我的Add_Executable方法。 這里:
add_executable (POC eb_pms.cpp url_binder.cpp)
所以這個問題看起來很小,但是,我無法找到任何解決方案。 或者,我應該說,沒有一篇文章以簡單的方式解釋它,所以我能理解。 我來自像C#/ Java這樣的強類型背景。 我正在嘗試使用Simple Web Server和RapidJSON在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();
};
}
網址綁定器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
我試圖研究已經有好幾個小時了。 但是我找不到那件丟失的東西。 任何幫助將不勝感激。
我只是在猜測 ,因為你沒有顯示你的CMakeLists.txt
文件......
我會說你沒有將你的url_binder.cpp
文件列為CMake add_executable
命令中的源。
必須列出您希望成為程序一部分的所有源文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.