kasom
kasom

Reputation: 43

Undefined reference to my function

I have some trouble with building the part of my project. Here it is:
utility.h:

#ifndef UTILITY_H
#define UTILITY_H

#include <boost/array.hpp>
#include <boost/shared_ptr.hpp>
#include <fstream>
#include <iostream>
#include <openssl/md5.h>
#include <string>

namespace utility {
    typedef boost::array<unsigned char, MD5_DIGEST_LENGTH> md5sum;
    typedef boost::shared_ptr<md5sum> md5sum_ptr;

    ...

    md5sum_ptr calculate_md5(const std::string& full_path);
};

#endif //UTILITY_H

utility.cpp:

#include "utility.h"

using namespace utility;

...

md5sum_ptr calculate_md5(const std::string& full_path) {
    md5sum_ptr md5(new md5sum);
    std::ifstream in(full_path);
    std::string content;
    in >> content;
    MD5((unsigned char*)content.data(), content.size(), md5->data());
    return md5;
}  

cdaemon.cpp

void CTCPConnection::handle_read_filename(ECommand command, EDataType datatype,
                      const boost::system::error_code& ec) {
    ...
        case ECommand::GET_MD5:
            _send_container<md5sum>(*(calculate_md5(filename)));
            async_write(m_socket, m_writebuf,
                    boost::bind(&CTCPConnection::handle_write_response, shared_from_this(),
                    placeholders::error));
            break;
        ...
    }
}  

Makefile:

CC=g++
override CFLAGS+=-c -std=c++11 -Wall -Wextra -Werror
override LFLAGS+=-lboost_log -lpthread -lboost_system -lboost_thread -lcrypto

server: servermain.o cdaemon.o
    $(CC) ../build/datatype.o ../build/utility.o build/servermain.o build/cdaemon.o -o build/frtpdaemon $(LFLAGS)
servermain.o: main.cpp
    $(CC) $(CFLAGS) main.cpp -o build/servermain.o
cdaemon.o: cdaemon.cpp
    $(CC) $(CFLAGS) -DBOOST_LOG_DYN_LINK cdaemon.cpp -o build/cdaemon.o   

When I try to build server, I get linker error:

build/cdaemon.o: In function `CTCPConnection::handle_read_filename(utility::ECommand, utility::EDataType, boost::system::error_code const&)':
cdaemon.cpp:(.text+0x127c): undefined reference to `utility::calculate_md5(std::string const&)'  

I have already tried to google the solution, but all what I could find were suggestions to change the order of ../build/utility.o and build/cdaemon.o. That didn't help me.

Upvotes: 1

Views: 943

Answers (1)

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153792

You don't define the function you have declared in the header. A using directive is insufficient when defining functions in a namespace. You'll either need to open the namespace before defining your function or qualify the function with the namespace, e.g.:

utility::md5sum_ptr utility::calculate_md5(const std::string& full_path) {
   ...
}

Upvotes: 2

Related Questions