C ++项目构建失败

时间:2015-03-20 17:30:28

标签: c++ macos

我编译项目时收到错误消息,我找到了these之类的Q& A,但它对我没有帮助。我不明白我的错误在哪里以及如何解决它。我使用的是Mac OS X,我有一个Xcode,但它不是Xcode项目。

我的错误:

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

我的树:

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

生成文件:

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

1 个答案:

答案 0 :(得分:1)

你的makefile很不稳定。过了一会儿,但试试这个:

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

.PHONY: build    
build: $(STDOUT)

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