c ++未定义的静态库引用 [英] c++ undefined references with static library

查看:841
本文介绍了c ++未定义的静态库引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个类,但试图使用它时,我总是得到错误与未定义的引用任何东西的静态库。我进行的方式是创建的对象文件像

  g ++ -c myClass.cpp -o myClass.o 

,然后用

包装

  ar rcs myClass.lib myClass.o 

有一些东西我显然缺少generaly这个..我打赌它的东西符号..
thx任何意见,我知道这可能是我可以找到,如果阅读一些教程,所以很抱歉,如果麻烦与愚蠢的东西:)



编辑:



myClass.h:

  class myClass {
public:
myClass();
void function();
};

myClass.cpp:

  #includemyClass.h

myClass :: myClass(){}
void myClass :: function(){}



程序使用以下类:

  #includemyClass.h

int main(){
myClass mc;
mc.function();

return 0;
}

最后编译成这样:

  g ++ -o main.exe -L。 -l myClass main.cpp 

错误只是经典:

  C:\Users\RULERO〜1\AppData\Local\Temp / ccwM3vLy.o:main.cpp :(。text + 0x31): undefined 
对`myClass :: myClass()'的引用
C:\Users\RULERO〜1\AppData\Local\Temp / ccwM3vLy.o:main.cpp :(。text + 0x3c):undefined
引用`myClass :: function()'
collect2:ld返回1退出状态


解决方案

这是可能一个链接顺序问题。当GNU链接器看到一个库时,它会丢弃它不需要的所有符号。在这种情况下,您的库将出现在.cpp文件之前,因此在编译.cpp文件之前将丢弃该库。请执行以下操作:

  g ++ -o main.exe main.cpp -L。 -lmylib 

  g ++ -o main.exe main.cpp myClass.lib 

不考虑命令行上的库的排序。


hi guys i'm trying to make a static library from a class but when trying to use it i always get errors with undefined references on anything. the way i proceeded was creating the object file like

g++ -c myClass.cpp -o myClass.o

and then packing it with

ar rcs myClass.lib myClass.o

there is something i'm obviously missing generaly with this.. i bet it's something with symbols.. thx for any advices, i know it's most probably something i could find out if reading some tutorial so sorry if bothering with stupid stuff again :)

edit:

myClass.h:

class myClass{
    public:
        myClass();
        void function();
};

myClass.cpp:

#include "myClass.h"

myClass::myClass(){}
void myClass::function(){}

program using the class:

#include "myClass.h"

int main(){
myClass mc;
mc.function();

return 0;
}

finally i compile it like this:

g++ -o main.exe -L. -l myClass main.cpp

the error is just classic:

C:\Users\RULERO~1\AppData\Local\Temp/ccwM3vLy.o:main.cpp:(.text+0x31): undefined
 reference to `myClass::myClass()'
C:\Users\RULERO~1\AppData\Local\Temp/ccwM3vLy.o:main.cpp:(.text+0x3c): undefined
 reference to `myClass::function()'
collect2: ld returned 1 exit status

解决方案

This is probably a link order problem. When the GNU linker sees a library, it discards all symbols that it doesn't need. In this case, your library appears before your .cpp file, so the library is being discarded before the .cpp file is compiled. Do this:

g++ -o main.exe main.cpp -L. -lmylib

or

g++ -o main.exe main.cpp myClass.lib

The Microsoft linker doesn't consider the ordering of the libraries on the command line.

这篇关于c ++未定义的静态库引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆