C++ 命名空间和链接不起作用
C++ Namespace and Linking not working
更新:已修复。非常简单的解决方案。我只需要添加我的 Add_Executable 方法。这里:
add_executable (POC eb_pms.cpp url_binder.cpp)
所以这个问题看起来很小,但是,我找不到任何解决方案。或者,我应该说,none 篇文章以简单的方式解释了它,因此我可以理解。我来自像 C#/Java 这样的强类型背景。我正在尝试使用 Simple Web Server and RapidJSON. Since we are building this project to be a part of a bigger solution, I cannot use commandline to link the program. I have to use the CMakeLists to compile and run the program. It means, the linking has to be done through the CMakeLists.txt file. The whole source code can be found here at GitHub 在 C++11 中构建 Rest API。我的环境是Macbook Pro + VS Code。所以我这里有两个 CPP:eb_pms.cpp、url_binder.cpp。 urlbinder 有一个命名空间 'EBPMS',eb_pms 有一个 none。以下是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 活页夹 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 活页夹 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;
}
}
所以我所需要的基本上就是找到一种方法 link 这个文件与我的原始 cpp 的 NameSpace 这样我就可以在我的主 cpp url binder 中使用方法 class.我正在尝试调用 eb_pms.cpp class 中的函数。当我 运行 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
命令中的源文件。
必须列出您希望成为程序一部分的所有源文件。
更新:已修复。非常简单的解决方案。我只需要添加我的 Add_Executable 方法。这里:
add_executable (POC eb_pms.cpp url_binder.cpp)
所以这个问题看起来很小,但是,我找不到任何解决方案。或者,我应该说,none 篇文章以简单的方式解释了它,因此我可以理解。我来自像 C#/Java 这样的强类型背景。我正在尝试使用 Simple Web Server and RapidJSON. Since we are building this project to be a part of a bigger solution, I cannot use commandline to link the program. I have to use the CMakeLists to compile and run the program. It means, the linking has to be done through the CMakeLists.txt file. The whole source code can be found here at GitHub 在 C++11 中构建 Rest API。我的环境是Macbook Pro + VS Code。所以我这里有两个 CPP:eb_pms.cpp、url_binder.cpp。 urlbinder 有一个命名空间 'EBPMS',eb_pms 有一个 none。以下是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 活页夹 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 活页夹 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;
}
}
所以我所需要的基本上就是找到一种方法 link 这个文件与我的原始 cpp 的 NameSpace 这样我就可以在我的主 cpp url binder 中使用方法 class.我正在尝试调用 eb_pms.cpp class 中的函数。当我 运行 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
命令中的源文件。
必须列出您希望成为程序一部分的所有源文件。