简体   繁体   English

如何解决链接器错误“找不到-lgcc_s”

[英]How to resolve linker error “cannot find -lgcc_s”

I have 3 tiny files which I use to make a static library and an app: 我有3个小文件,可用来制作静态库和一个应用程序:

test.h 测试

#ifndef TEST_H
#define TEST_H

class Test
{
    public:
        Test();
};

extern Test* gpTest;

#endif

test.cpp 测试文件

#include "test.h"

Test::Test()
{
    gpTest = this;
}   

Test test;

main.cpp main.cpp

#include "test.h"
#include <iostream>

using namespace std;

Test* gpTest = NULL;

int main()
{
    return 0;
}

BUILD 建立

g++ -c test.cpp -o test.o
ar cr test.a test.o
g++ -c main.cpp -o main.o
g++ main.o -o app -Wl,--whole-archive -L/home/dumindara/intest/test.a -Wl,-no--whole-archive

ERROR (linking step) 错误(链接步骤)

/usr/lib64/gcc/x86_64-suse-linux/4.3/../../../../x86_64-suse-linux/bin/ld: cannot find -lgcc_s
collect2: ld returned 1 exit status

I tried everything: using -static-libgcc and linking to static libstdc++. 我尝试了所有事情:使用-static-libgcc并链接到静态libstdc ++。 Can't get this to work. 无法使它正常工作。 It is all due to --whole-archive flag. 这都是由于--whole-archive标志。 But I can't do without it. 但是我不能没有它。

You have a typo. 你有错字 -no--whole-archive should be --no-whole-archive. -no-整体存档应为-no-整体存档。 Fixing the typo fixes the linker error. 修正拼写错误可修复链接器错误。

I think -- is the problem here: 我认为--是这里的问题:

-Wl,-no--whole-archive

Try with 试试看

-Wl,-no-whole-archive

edit 编辑

About not seeing the test symbol in your app with nm app: I think you don't need -L since you give full path and name of test.a - do either 关于在nm应用程序中没有在您的应用程序中看到测试符号:我认为您不需要-L因为您提供了完整的测试路径和名称。

-Wl,--whole-archive -L/home/dumindara/intest/ -ltest -Wl,-no-whole-archive

or 要么

-Wl,--whole-archive /home/dumindara/intest/test.a -Wl,-no-whole-archive

With regards to the comments so far: just drop the -Wl option entirely. 关于到目前为止的评论:只需完全删除-Wl选项即可。 Let g++ do its thing. 让g ++做事情。

As for not finding the symbol test with nm, why would you expect anything else? 至于找不到用nm进行的符号测试,为什么还要其他呢? You don't use it, your program doesn't need it, so it isn't pulled in. (If for some reason you need to include an object file which isn't referenced, eg because static initializers will make it visible, then specify the object file---don't put it in a library, which is the standard way of saying don't include it unless needed.) 您不使用它,您的程序不需要它,因此它不会被拉入。(如果由于某种原因,您需要包括一个未引用的目标文件,例如,因为静态初始化程序使其可见,然后指定目标文件--不要将其放在库中,这是标准的表达方式,除非需要,否则不要包含它。

-- James Kanze 詹姆斯·坎泽

whole-archive works, you are just not linking the lib. 整个存档作品,您只是没有链接lib。 The correct thing to do is : 正确的做法是:

ar cr libtest.a test.o

and link with 并与

-Wl,--whole-archive -L/home/dumindara/intest/ -ltest

请注意:整体归档仅适用于静态库( .a),不适用于共享库( .so),这是我的经验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM