rel1x
rel1x

Reputation: 2441

C++ project build failed

I've got an error message when I compile my project and I find same Q&A like these but its doesn't help me. I don't understand where is my mistake and how I can fix it. I use Mac OS X and I have an Xcode, but its not Xcode project.

My error:

g++ cleanup_agent.cc -o cleanup_agent -lcurl
Undefined symbols for architecture x86_64:
  "reader(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
      _main in cleanup_agent-12c8ac.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: *** [build] Error 1

My tree:

.
├── Makefile
├── bad_words.txt
├── cleanup_agent.cc
├── src
│   ├── reader.cc
│   ├── reader.h
│   └── stdafx.h
└── test
    └── unittest.cc

Makefile:

CC = g++
STDIN = cleanup_agent.cc
STDOUT = cleanup_agent

build: $(STDIN)
    $(CC) $(STDIN) -o $(STDOUT) -lcurl

cleanup_agent.cc:

#include "src/stdafx.h"
#include "src/reader.h"


int main() {
  vector<string> bad_words;
  bad_words = reader("bad_words.txt");
}

reader.h:

#ifndef CLEANUP_AGENT_SRC_READER_H_
#define CLEANUP_AGENT_SRC_READER_H_

vector<string> reader(string filename);

#endif

reader.cc:

#include "stdafx.h"

#include "reader.h"
#include <fstream>

vector<string> reader(string filename) {
  vector<string> data;

  fstream file;
  file.open(filename, fstream::in);

  if (file.good()) {
    string line;
    while (getline(file, line)) {
      data.push_back(line);
    }
  } else {
    cout << "file not found." << endl;
  }

  file.close();

  return data;
}

Upvotes: 1

Views: 74

Answers (1)

Matthew Moss
Matthew Moss

Reputation: 1248

Your makefile is wonky. Been a while, but try this:

CC = g++
STDIN = cleanup_agent.cc src/reader.cc
STDOUT = cleanup_agent

.PHONY: build    
build: $(STDOUT)

$(STDOUT): $(STDIN)
    $(CC) $(STDIN) -o $(STDOUT) -lcurl

Upvotes: 1

Related Questions