user277465
user277465

Reputation:

C++ linking error upon using "string"

I'm working on a hobby side mini project in C++ and I have the following error upon running my binary.

Undefined symbols for architecture x86_64:
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(char const*, unsigned long)", referenced from:
      recv_line(int) in networking.o
ld: symbol(s) not found for architecture x86_64

I have a file networking.cpp which has uses an instance of "std::string". I have the following contents inside my networking.h

#ifndef _NETWORKING_H
#define _NETWORKING_H

#include <iostream>
#include <string>

#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define DELIMITER '\n'

std::string recv_line(int sockfd);
#endif

Inside networking.cpp I merely have :-

std::string recv_line(int sockfd) {
return "";
}

What am I doing wrong?

[EDIT]

I'm making the file as follows :-

% cat Makefile
CXX=g++

blah: main networking
    $(CXX) main.o networking.o -o blah -v
main:
    $(CXX) -c main.cpp
networking:
    $(CXX) -c networking.cpp
% make

The version of g++ seems to be :-

% g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix

Upvotes: 1

Views: 2951

Answers (1)

doptimusprime
doptimusprime

Reputation: 9405

You need to link stdc++ library to your program. This you can do here.

$(CC) main.o networking.o -o blah -v -lstdc++.

Error here shows that libstdc++ is not being linked your program.

Also, in make file, use $(CC) instead of CC.

Upvotes: 4

Related Questions