简体   繁体   中英

C++ Namespace and Linking not working

Update: Fixed. Very simple solution. I just have to add in my Add_Executable method. Here:

add_executable (POC eb_pms.cpp url_binder.cpp)

So this problem looks pretty small, yet, I am unable to find any solution of this. Or, I should say, none of articles explains it in a simple manner so I can understand. I have come from a strongly typed background like C#/Java. I am trying to build a Rest API in C++11 using 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 . My environment is Macbook Pro + VS Code. So I have two CPPs here: eb_pms.cpp, url_binder.cpp. The urlbinder has a namespace 'EBPMS' and eb_pms has none. Following is the code of ep_pms:

(header file)

#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 file:

# 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();
}

the url binder 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();
    }; 
} 

the 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;
    }
}

So all I need is basically, to find a way to link this file with NameSpace with my original cpp so I can use the method from url binder inside my main cpp class. I am trying to call function in eb_pms.cpp class. When I run make command, I get this error:

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

It has been hours since I am trying to research. But I am unable find that missing piece. Any help will be greatly appreciated.

I'm just guessing here since you don't show your CMakeLists.txt file...

I would say that you don't list your url_binder.cpp file as a source in the CMake add_executable command.

All source-files you want to be part of your program must be listed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM