jeudi 25 mai 2023

How can I fix linker error 'Undefined symbols for architecture arm64' when importing namespaces in C++ on a Mac M1?

I try to import functoin to server.cpp from routes.cpp file. If I put all code in one file, it will compile successfully. I use MAC M1 to develop. I get the error message.

Undefined symbols for architecture arm64:
  "handleRoutes::paresHttpReq(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>)", referenced from:
      tcp_connection::start() in main.cpp.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

This is my file structure.

-blog  
  -routes
    -mainRoutes.h
    -routes.cpp
  -TCPconnection
    -server.cpp
  -main.cpp
  -CMakeLists.txt

mainRoutes.h

#pragma once
#ifndef _MAINROUTES_H_
#define _MAINROUTES_H_

#include <string>

namespace handleRoutes
{
    void paresHttpReq(const std::string);

}

#endif

routes.cpp

#include "mainRoutes.h"
#include <iostream>
#include <vector>
#include <string>
#include <boost/algorithm/string.hpp>

namespace handleRoutes
{
    void paresHttpReq(const std::string &rawRequest) {...}

}

server.cpp

#include <iostream>
#include <string>
#include <boost/bind/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include <boost/algorithm/string.hpp>
#include "../routes/mainRoutes.h"
        
        ...
        std::array<char, 1024> buffer;
        boost::system::error_code readError;
        size_t bytesRead = socket_.read_some(boost::asio::buffer(buffer), readError);
        std::string rawRequest(buffer.data(), bytesRead);
        handleRoutes::paresHttpReq(rawRequest); <--- error
        ...

CMakeLists.txt

cmake_minimum_required(VERSION 3.12)
project(Blog CXX)

set(CMAKE_CXX_STANDARD 11)

add_library(mainRoutes OBJECT ./routes/mainRoutes.h)

add_executable(main main.cpp $<TARGET_OBJECTS:mainRoutes>)

find_package(Boost REQUIRED COMPONENTS system)

if(Boost_FOUND)
  target_link_libraries(main PRIVATE Boost::system)
endif()

include_directories(${Boost_INCLUDE_DIRS})

I check header files is exist. The namespace is same in all file.

Aucun commentaire:

Enregistrer un commentaire