标签: undefined-reference

gcc:未定义的引用

我想编译一下.

program.c

#include <libavcodec/avcodec.h>

int main(){
    int i = avpicture_get_size(AV_PIX_FMT_RGB24,300,300);
}
Run Code Online (Sandbox Code Playgroud)

运行这个

gcc -I$HOME/ffmpeg/include program.c
Run Code Online (Sandbox Code Playgroud)

给出错误

/tmp/ccxMLBme.o: In function `main':
program.c:(.text+0x18): undefined reference to `avpicture_get_size'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

但是,定义了avpicture_get_size.为什么会这样?

c c++ linker gcc undefined-reference

39
推荐指数
2
解决办法
14万
查看次数

如何在链接时重新定位__gcov_init未定义的引用问题

我现在正在进行C代码覆盖率研究并遇到以下问题,GCC版本4.4.6:

  1. 添加了编译器标志CFLAGS = --coverage和链接器选项LDFLAGS := --coverageLOCAL_LDLIBS := --coverage得到错误:

undefined reference to '__gcov_init'" and "undefined reference to '__gcov_merge_add'

  1. 添加选项LOCAL_LDFLAGS := --coverage,并收到链接错误:

libgcov.a(_gcov.o): in function __gcov_set_sampling_rate: undefined reference to '__gcov_sampling_rate' libgcov.a(_gcov.o): in function gcov_exit: undefined reference to '__gcov_pmu_profile_filename' libgcov.a(_gcov.o): in function __gcov_init: undefined reference to '__gcov_pmu_profile_options' '__gcov_pmu_top_n_address'

有人可以帮忙就这个问题提供一些建议吗?

linker gcov undefined-reference

38
推荐指数
3
解决办法
3万
查看次数

虽然符号出现在*.so文件中,但是g ++未定义引用

我发现了一些类似的问题(如这个,那个这个),但没有人帮我解决我的问题.我有一个*.so文件(来自gnss-sdr的核心),如下所示:

$nm libgnss_system_parameters_dyn.so  | c++filt  |grep Gps_Eph
Run Code Online (Sandbox Code Playgroud)

包含符号Gps_Ephemeris::Gps_Ephemeris(),它应该是一个构造函数.

我写了一些最小的代码:

#include <iostream>
#include <core/system_parameters/gps_ephemeris.h>

int main(int argc,const char* argv[])
{
    Gps_Ephemeris ge;
    return 0; 
}
Run Code Online (Sandbox Code Playgroud)

我编译的是:

g++  main.cpp -std=c++0x -I some_include_path -L some_lib_path -l gnss_system_parameters_dyn`
Run Code Online (Sandbox Code Playgroud)

然后链接器抱怨:

/tmp/ccHCvldG.o: In function `main':
main.cpp:(.text+0x33): undefined reference to `Gps_Ephemeris::Gps_Ephemeris()'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

我也尝试过cmake,但它生成的行类似于它(它只是-rdynamic在链接之前添加),它仍然生成完全相同的链接器错误.

请注意,库和我的最小代码都使用相同的编译器(g ++ - 5)进行编译,具有完全相同的标志和相同的c ++ 0x标准.


解决Maxim Egorushkin的答案:

nm --demangle --defined-only --extern-only libgnss_system_parameters.so  |grep …
Run Code Online (Sandbox Code Playgroud)

c++ g++ name-mangling undefined-reference

36
推荐指数
2
解决办法
3562
查看次数

crt1.o:在函数`_start'中: - Linux中对'main'的未定义引用

我正在将应用程序从Solaris移植到Linux

链接的目标文件没有定义main().但是在Solaris中正确地完成了编译和链接,并生成了可执行文件.在Linux中我得到这个错误

    /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main' 
Run Code Online (Sandbox Code Playgroud)

我的问题是,我不能包含新的.c/.o文件,因为它是一个巨大的应用程序,并已运行多年.我怎样才能摆脱这个错误?

makefile的代码提取:

RPCAPPN = api
LINK = cc 

    $(RPCAPPN)_server: $(RPCAPIOBJ)
            $(LINK) -g $(RPCAPIOBJ) -o $(RPCAPPN)_server $(IDALIBS) $(LIBS) $(ORALIBS) $(COMMONLIB) $(LIBAPI) $(CCLIB) $(THREADLIB) $(DBSERVERLIB) $(ENCLIB)
Run Code Online (Sandbox Code Playgroud)

linux program-entry-point undefined-reference

34
推荐指数
3
解决办法
7万
查看次数

未定义的引用`__stack_chk_fail'

编译C++代码时出现此错误:

undefined reference to `__stack_chk_fail'
Run Code Online (Sandbox Code Playgroud)

已尝试的选项:

  1. 在编译时添加-fno-stack-protector - 不起作用,错误仍然存​​在
  2. 在我的代码中添加了一个void __stack_chk_fail(void)的虚拟实现.仍然得到同样的错误.

详细错误:

/u/ac/alanger/gurobi/gurobi400/linux64/lib/libgurobi_c++.a(Env.o)(.text+0x1034): In function `GRBEnv::getPar/u/ac/alanger/gurobi/gurobi400/linux64/lib/libgurobi_c++.a(Env.o)(.text+0x1034): In function `GRBEnv::getParamInfo(GRB_StringParam, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
: undefined reference to `__stack_chk_fail'
amInfo(GRB_StringParam, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
: **undefined reference to `__stack_chk_fail'**
Run Code Online (Sandbox Code Playgroud)

早些时候,我得到了10个这样的错误.发现gcc我正在使用的预编译库和gcc我用来编译代码的版本之间存在版本不匹配.更新了gcc,现在我只收到其中2个错误.

有什么帮助吗?

c++ gcc undefined-reference

29
推荐指数
1
解决办法
4万
查看次数

如何正确地将库与cmake链接?

我无法使用我正在使用的其他库来正确链接到我的项目中.

我正在使用CLion,它使用cmake来构建它的项目.我试图将几个库与OpenGL结合使用来构建一些对象.我最初在Visual Studio中构建它,因为我无法弄清楚如何让cmake与Clion一起工作.但是,现在代码全部工作(无论如何都在Visual Studio中),我希望能够使用CLion,因为这是我首选的IDE.

我还是cmake的新手,我不明白我的错误CMakeLists.txt.这是我有的:

cmake_minimum_required(VERSION 3.3)
project(texture_mapping)
find_package(OpenGL REQUIRED)
link_directories(${OPENGL_gl_LIBRARY})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp camera.h display.h display.cpp mesh.cpp mesh.h obj_loader.cpp obj_loader.h shader.cpp shader.h stb_image.c stb_image.h texture.cpp texture.h transform.h)

link_directories(texture_mapping ${PROJECT_SOURCE_DIR}/lib)

add_executable(texture_mapping ${SOURCE_FILES})

target_include_directories(texture_mapping PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(texture_mapping SDL2 SDL2main SDL2test glew32 glew32s ${OPENGL_gl_LIBRARY})
Run Code Online (Sandbox Code Playgroud)

我调整它直到它没有在CLion中给我任何更多的错误,但我的代码仍然无法识别头文件.

这是我的项目的结构:
项目结构

所以,我放了我需要的所有库,但它似乎没有在代码中识别它们.Clion在项目中识别它们(它们没有出现错误的红色),但是当它构建时(当我尝试在CLion中运行它时),我得到这些错误:

CMakeFiles\texture_mapping.dir/objects.a(mesh.cpp.obj): In function `ZN4MeshD2Ev':
...texture-mapping/mesh.cpp:30: undefined reference to `_imp____glewDeleteVertexArrays'
CMakeFiles\texture_mapping.dir/objects.a(mesh.cpp.obj): In function `ZN4Mesh8InitMeshERK12IndexedModel':
...texture-mapping/mesh.cpp:36: undefined reference to `_imp____glewGenVertexArrays'
...texture-mapping/mesh.cpp:37: undefined reference to `_imp____glewBindVertexArray'
...texture-mapping/mesh.cpp:39: undefined reference to `_imp____glewGenBuffers'
...texture-mapping/mesh.cpp:40: undefined reference …
Run Code Online (Sandbox Code Playgroud)

c++ opengl cmake undefined-reference

29
推荐指数
1
解决办法
5万
查看次数

C++模板,未定义的引用

我有一个声明如下的函数:

template <typename T> 
T read();
Run Code Online (Sandbox Code Playgroud)

并定义如下:

template <typename T>
T packetreader::read() {
    offset += sizeof(T);
    return *(T*)(buf+offset-sizeof(T)); 
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试在main()函数中使用它时:

packetreader reader;
reader.read<int>();
Run Code Online (Sandbox Code Playgroud)

我从g ++得到以下错误:

g++ -o main main.o packet.o
main.o: In function `main':
main.cpp:(.text+0xcc): undefined reference to `int packetreader::read<int>()'
collect2: ld returned 1 exit status
make: *** [main] Error 1
Run Code Online (Sandbox Code Playgroud)

有人能指出我正确的方向吗?

c++ templates g++ undefined-reference

28
推荐指数
3
解决办法
3万
查看次数

C++未定义对已定义函数的引用

我无法弄清楚为什么这不起作用.我会把我的所有三个文件都拿出来,也许有人可以告诉我它为什么会抛出这个错误.我正在使用g ++来编译程序.

程序:

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

using namespace std;

int main()
{
  char sentence[MAX_SENTENCE_LENGTH];
  char writeTo[] = "output.txt";
  int distanceTo,likePosition, length, numWords;
  cout << "ENTER A SENTENCE!   ";
  cin.getline(sentence, 299);
  length = strlen(sentence);
  numWords = wordCount(sentence, length);
  for(int x = 0; x < 3; ++x)
  {
    likePosition = likePos(numWords);
    distanceTo = lengthTo(sentence, likePosition, length);
    insertLike(sentence, distanceTo, length, writeTo);
  }
  return 0;  
}
Run Code Online (Sandbox Code Playgroud)

功能文件:

void insertLike(const char sentence[],  const int lengthTo, const int length, char writeTo[])
{
  char part1[MAX_SENTENCE_LENGTH], part2[MAX_SENTENCE_LENGTH]; …
Run Code Online (Sandbox Code Playgroud)

c++ g++ undefined-reference

27
推荐指数
4
解决办法
10万
查看次数

使用Libc ++未定义的引用与Clang进行编译

第一对夫妇太长,无法参考.当我尝试clang++ -stdlib=libc++ ../main.cc ...使用SVN中的clang和libc ++ 进行编译时,我收到此错误.

error: undefined reference to 'typeinfo for char const*'
error: undefined reference to '__cxa_allocate_exception'
error: undefined reference to '__cxa_throw'
/tmp/cc-pbn00y.o:../main.cc:function std::__1::deque<double, std::__1::allocator<double> >::__add_back_capacity(): error: undefined reference to '__cxa_begin_catch'
/tmp/cc-pbn00y.o:../main.cc:function std::__1::deque<double, std::__1::allocator<double> >::__add_back_capacity(): error: undefined reference to '__cxa_rethrow'
/tmp/cc-pbn00y.o:../main.cc:function std::__1::deque<double, std::__1::allocator<double> >::__add_back_capacity(): error: undefined reference to '__cxa_end_catch'
/tmp/cc-pbn00y.o(.eh_frame+0xbd3): error: undefined reference to '__gxx_personality_v0'
Run Code Online (Sandbox Code Playgroud)

解决方案:感谢其中一个答案,我知道解决方案.libc ++本身不能像libstdc ++那样使用,它必须与libc ++ abi一起链接.但是,libc ++ abi尚未完成,因此目前使用libc ++似乎有些不完整,但它仍然是我完成时的首选.

更新2012年5月26日: libc ++ abi现在已经完成了C++,我已经成功地使用了clang ++ clang++ -std=c++11 -stdlib=libc++ …

c++ std clang undefined-reference libc++

27
推荐指数
1
解决办法
3万
查看次数

Virtual Base类析构函数的"未定义引用"

可能重复:
什么是未定义的引用/未解析的外部符号错误,如何解决?

我有一些Java经验,现在正在做C++课程.我想尝试编写一个界面,但是我遇到了一些我无法解决的析构函数问题,即使是在Internet上的帮助......这是我的代码:

    class Force {

    public:

    virtual ~Force();
    virtual VECTOR eval(VECTOR x, double t);

};

class InvSquare : public Force {

    public:

    InvSquare(double A) {

        c = A;

    }

    ~InvSquare(){};

    VECTOR eval(VECTOR x, double t) { // omitted stuff }

    private:
    double c;

};
Run Code Online (Sandbox Code Playgroud)

我试图为基类声明一个虚拟析构函数,并为派生类声明一个非虚函数,但是我得到一个错误,说"未定义引用`Force :: ~Force()'".它是什么意思,我该如何解决?

如果这是一个愚蠢的问题,请原谅我!

非常感谢你的帮助,noctilux

c++ abstract-class undefined-reference

27
推荐指数
1
解决办法
3万
查看次数