【发布时间】:2020-04-01 10:32:03
【问题描述】:
我是 C++ 新手(以及一般的编译语言),并且正在 Bjarne Stroustrup “使用 C++ 编程和实践”的第 8 章末尾进行练习,但是当我尝试编译时出现以下错误代码
➜ Desktop g++ -std=c++11 *.cpp -o use
Undefined symbols for architecture x86_64:
"_foo", referenced from:
print_foo() in my-4f7853.o
_main in use-46cb26.o
(maybe you meant: __Z9print_foov)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我也尝试过使用g++ -c my.cpp use.cpp,后跟g++ -o use.exe my.o use.o,但这给出了同样的错误。我尝试的另一种方法是g++ -c use.cpp -o use.exe,但是use.exe 在运行时没有产生任何输出。源代码文件是
我的.h
extern int foo;
void print_foo();
void print_int(int);
我的.cpp
#include "my.h"
#include <iostream>
void print_foo() {
std::cout << foo << '\n';
}
void print_int(int num) {
std::cout << num << '\n';
}
使用.cpp
#include "my.h"
#include <iostream>
int main() {
std::cout<<"DSGFSGFSG"<< '\n';
foo = 7;
print_foo();
int i = 99;
print_int(i);
}
我查看了其他类似的问题(如果在Link-time errors in VS 2013 while compiling the C++ program - B. Stroustrup's PPP using C++: Ch. 8 - Q1 Drill? 中看起来不一样),但解决方案对我不起作用。问题与我使用 g++ 的编译有关还是我犯了更根本的错误?
【问题讨论】:
-
请不要发布带有行号前缀的代码——它们并没有真正的帮助,而且它们会让其他人更难以复制您的代码并自己运行。
标签: c++ g++ clang linker-errors