C++ 链接到静态库时出现未定义的符号错误

C++ 链接到静态库时出现未定义的符号错误,c++,linker,static-libraries,static-linking,C++,Linker,Static Libraries,Static Linking,我有一个项目1,它编译成libtest.a库。它包含test.cpp和test.h,如下所示 试验h包括: void test(); #include "test.h" void test() {} #include "../test/test.h" int main() { test(); } test.cpp包含: void test(); #include "test.h" void test() {} #include "../test/test.h" int ma

我有一个项目1,它编译成libtest.a库。它包含test.cpp和test.h,如下所示

试验h包括:

void test();
#include "test.h"
void test() {}
#include "../test/test.h"

int main() {
    test();
}
test.cpp包含:

void test();
#include "test.h"
void test() {}
#include "../test/test.h"

int main() {
    test();
}
libtest.a编译时没有错误或警告

我还有另一个项目2,它链接到项目1中的libtest.a,并编译成可执行二进制文件。它包含main.cpp,如下所示

main.cpp包含:

void test();
#include "test.h"
void test() {}
#include "../test/test.h"

int main() {
    test();
}
../test/test.h是从项目2中的main.cpp到项目1中的test.h的相对路径

当我编译项目2时,我得到

Undefined symbols for architecture x86_64:
  "test()", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

如果我完全删除test的实现,并且将定义放在test.h而不是test.cpp中,那么我会得到相同的错误。这使我相信项目2可以看到test.h,但不能看到test.cpp。为什么?我如何解决这个问题?

你的构建命令行是什么?@LuchianGrigore我不确定那是什么。@拟人化:他的意思是你用什么命令来编译和链接代码?你能把它加到问题上吗?即使您是从IDE进行构建,那么只要单击“构建”按钮,您应该仍然可以在某处看到它。有人知道在Xcode中的何处可以找到该信息吗?可能是愚蠢的问题:您是否记得将库本身添加到您的Xcode项目中?