【问题标题】:Geany - undefined referenceGeany - 未定义的参考
【发布时间】:2013-09-30 00:33:24
【问题描述】:

我正在尝试用 Geany 编译 C++ 代码。

编译命令:g++ -Wall -c "%f"

构建命令:g++ -Wall -o "%e" "%f"

main.cpp:

#include <iostream>

#include "Person.hpp"

int main()
{
    Person p1(16);

    std::cout << p1.getAge();

    return 0;
}

Person.hpp

class Person
{
public:

    Person(int a);

    void setAge(int);

    int getAge() const;

private:

    int age;
};

inline int Person::getAge() const
{
    return age;
}

Person.cpp

#include "Person.hpp"

Person::Person(int a)
{
    age = a;
}

void Person::setAge(int a)
{
    age = a;
}

错误:

g++ -Wall -o "main" "main.cpp" (在目录中: /home/me/projects/Test) /tmp/ccxYmWkE.o: 在函数main': main.cpp:(.text+0x15): undefined reference toPerson::Person(int)' collect2: error: ld 返回 1 exit status 编译失败。

在 Geany 之前,我只使用 Code::Blocks 并且一切正常。我该如何解决?

【问题讨论】:

  • 您可以(并且可能应该)配置 Geany 为 GNU 使用 Makefile make;您的构建命令显然是错误的,您需要链接几个目标文件...

标签: c++ compiler-construction reference undefined geany


【解决方案1】:

很明显你没有在编译命令中添加Person.cpp。则无法通过联动关卡。

-o Person Person.cpp 添加到g++ -Wall -c "%e" "%f" 之后的构建选项中。 毕竟编译命令应该是这样的:

g++ -Wall -o "main" "main.cpp" -o Person Person.cpp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 2017-10-13
    • 1970-01-01
    • 2014-12-16
    • 2015-01-14
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多