我用几个创建了我的lib.a文件
gcc -c file.c -o file.o
Run Code Online (Sandbox Code Playgroud)
然后
ar sr lib/libtest.a file1.o file2.o file3.o
Run Code Online (Sandbox Code Playgroud)
确认
ar -t lib/libtest.a
file1.o
file2.o
file3.o
Run Code Online (Sandbox Code Playgroud)
但是当我尝试编译测试应用程序时
gcc lib/libtest.a test.c -o test
Run Code Online (Sandbox Code Playgroud)
我在函数中得到了未定义的引用main:来自file1.o,file2.o,file3.o的函数
我在编译 odbc sdk 中的一些示例时遇到问题。经过一段时间与库订单的混合后,我以某种方式设法获得了其中少数未定义引用的数量。
可悲的是,我不知道如何摆脱剩下的。这是失败的命令:
g++ -Wall -z defs -m64 -DSIMBA -D_REENTRANT -fPIC -O0 -g -shared Common/TabbedUnicodeFileReader_Linux_x8664_debug.cpp.o Core/QSConnection_Linux_x8664_debug.cpp.o Core/QSDriver_Linux_x8664_debug.cpp.o Core/QSEnvironment_Linux_x8664_debug.cpp.o Core/QSStatement_Linux_x8664_debug.cpp.o DataEngine/QSDataEngine_Linux_x8664_debug.cpp.o DataEngine/QSMetadataHelper_Linux_x8664_debug.cpp.o DataEngine/QSTable_Linux_x8664_debug.cpp.o DataEngine/QSTableUtilities_Linux_x8664_debug.cpp.o DataEngine/QSTypeInfoMetadataSource_Linux_x8664_debug.cpp.o Common/QSTableMetadataFile_Unix_Linux_x8664_debug.cpp.o Common/QSUtilities_Unix_Linux_x8664_debug.cpp.o Main_Unix_Linux_x8664_debug.cpp.o -Wl,--no-undefined -Wl,--no-allow-shlib-undefined -Wl,--whole-archive,/home/hector/Downloads/SimbaEngineSDK/9.0/DataAccessComponents//Lib/Linux_x8664/libSimbaDSI_debug.a,/home/hector/Downloads/SimbaEngineSDK/9.0/DataAccessComponents//Lib/Linux_x8664/libSimbaSupport_debug.a,/home/hector/Downloads/SimbaEngineSDK/9.0/DataAccessComponents//Lib/Linux_x8664/libAEProcessor_debug.a,/home/hector/Downloads/SimbaEngineSDK/9.0/DataAccessComponents//Lib/Linux_x8664/libCore_debug.a,/home/hector/Downloads/SimbaEngineSDK/9.0/DataAccessComponents//Lib/Linux_x8664/libDSIExt_debug.a,/home/hector/Downloads/SimbaEngineSDK/9.0/DataAccessComponents//Lib/Linux_x8664/libExecutor_debug.a,/home/hector/Downloads/SimbaEngineSDK/9.0/DataAccessComponents//Lib/Linux_x8664/libParser_debug.a,/home/hector/Downloads/SimbaEngineSDK/9.0/DataAccessComponents//Lib/Linux_x8664/libSimbaODBC_debug.a -Wl,--no-whole-archive -Wl,--soname=../Bin/Linux_x8664/libQuickstart_debug.so -L/home/hector/Downloads/SimbaEngineSDK/9.0/DataAccessComponents//ThirdParty/icu/Linux_x8664/lib -licuuc_simba64 -licudata_simba64 -licui18n_simba64 -lpthread -lm -lc -ldl -Wl,--version-script=exports_Linux.map -o ../Bin/Linux_x8664/libQuickstart_debug.so
Run Code Online (Sandbox Code Playgroud)
编辑:缺少符号
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/libdl.so: undefined reference to `_dl_rtld_di_serinfo@GLIBC_PRIVATE'
/lib/x86_64-linux-gnu/libpthread.so.0: undefined reference to `_dl_allocate_tls_init@GLIBC_PRIVATE'
/lib/x86_64-linux-gnu/libpthread.so.0: undefined reference to `__libc_stack_end@GLIBC_2.2.5'
/lib/x86_64-linux-gnu/libpthread.so.0: undefined reference to `_dl_get_tls_static_info@GLIBC_PRIVATE'
/lib/x86_64-linux-gnu/libpthread.so.0: undefined reference to `__tls_get_addr@GLIBC_2.3'
/lib/x86_64-linux-gnu/libpthread.so.0: undefined reference to `_dl_deallocate_tls@GLIBC_PRIVATE'
/lib/x86_64-linux-gnu/libpthread.so.0: undefined reference to …Run Code Online (Sandbox Code Playgroud) LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include<iostream>
template<class T> class LinkedList;
//------Node------
template<class T>
class Node {
private:
T data;
Node<T>* next;
public:
Node(){data = 0; next=0;}
Node(T data);
friend class LinkedList<T>;
};
//------Iterator------
template<class T>
class Iterator {
private:
Node<T> *current;
public:
friend class LinkedList<T>;
Iterator operator*();
};
//------LinkedList------
template<class T>
class LinkedList {
private:
Node<T> *head;
public:
LinkedList(){head=0;}
void push_front(T data);
void push_back(const T& data);
Iterator<T> begin();
Iterator<T> end();
};
#endif /* LINKEDLIST_H */
Run Code Online (Sandbox Code Playgroud)
LinkedList.cpp
#include "LinkedList.h" …Run Code Online (Sandbox Code Playgroud) 我有三个文件:myh.h; my.cpp; use.cpp.以下是文件的内容:
myh.h
extern int foo;
void print_foo();
void print(int);
Run Code Online (Sandbox Code Playgroud)
my.cpp
#include "myh.h"
#include <iostream>
void print_foo()
{
std::cout<<foo<<std::endl;
}
void print(int i)
{
std::cout<<i<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
use.cpp
#include "myh.h"
int main()
{
foo=7;
print_foo();
print(99);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
GCC发出以下错误:
my.o:my.cpp:(.text+0x7): undefined reference to `foo'
use.o:use.cpp:(.text+0x10): undefined reference to `foo'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
我使用-c命令编译文件,它不会给出错误.我使用以下命令进行链接:
g++ -o final my.o use.o
Run Code Online (Sandbox Code Playgroud)
这里有什么问题,我读了类似问题的其他主题,这里的情况很奇怪.....
好奇的是,这是Stroustrup的"使用C++编程原则"一书中的练习练习
编辑:我做了像dasblinkenlight说的那样,并且在use.cpp中我在foo前添加了一个int(所以现在定义了foo),但我仍然得到这个错误:
my.o:my.cpp:(.text+0x7): undefined reference to `foo'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
哪个告诉我它在my.cpp中也没有定义呢?如果我必须在任何地方定义它在头文件中包含它的重点是什么,或者如何更恰当地接近它?
沿着makefile运行,我得到了这个:
cc client.o MurmurHash3.o libstorage.a -Wall -lreadline -pthread -o client
MurmurHash3.o: In function `MurmurHash3_x64_128':
/home/evantandersen/mount/src/MurmurHash3.c:59: undefined reference to `rotl64'
/home/evantandersen/mount/src/MurmurHash3.c:106: undefined reference to `fmix'
Run Code Online (Sandbox Code Playgroud)
并且,MurmurHash3.c:
inline uint64_t rotl64 ( uint64_t x, int8_t r )
{
return (x << r) | (x >> (64 - r));
}
//-----------------------------------------------------------------------------
// Finalization mix - force all bits of a hash block to avalanche
inline uint64_t fmix ( uint64_t k )
{
k ^= k >> 33;
k *= 0xff51afd7ed558ccd;
k ^= …Run Code Online (Sandbox Code Playgroud) 我试图在Win 7 x64系统上使用MinGW编译一个相对简单的OpenGL程序,并且我不断获得对几个GLEW函数的未定义引用.我已将库设置为链接到程序,并且一直在寻找我列表中可能缺少的任何库,但链接器的输出仍然如下所示:
16:35:50 **** Incremental Build of configuration Debug for project test ****
Info: Internal Builder is used for build
gcc -LD:/DEV/openGL/lib/x86 -LD:/DEV/x86/lib -o test.exe test.o -lfreeglut -lglaux -lglew32s -lglu32 -lglfw3 -lopengl32 -lgdi32
test.o: In function `init':
E:\Development\C\test\Debug/../test.c:32: undefined reference to `_imp____glewGenVertexArrays'
E:\Development\C\test\Debug/../test.c:33: undefined reference to `_imp____glewBindVertexArray'
E:\Development\C\test\Debug/../test.c:35: undefined reference to `_imp____glewGenBuffers'
E:\Development\C\test\Debug/../test.c:36: undefined reference to `_imp____glewBindBuffer'
E:\Development\C\test\Debug/../test.c:37: undefined reference to `_imp____glewBufferData'
test.o: In function `display':
E:\Development\C\test\Debug/../test.c:45: undefined reference to `_imp____glewBindVertexArray'
test.o: In function `main':
E:\Development\C\test\Debug/../test.c:61: undefined reference to …Run Code Online (Sandbox Code Playgroud) 我正在尝试编译依赖于某些SSL库的C程序。当我尝试编译时,出现以下错误:
michael@michael-VirtualBox:~/$ cc -lssl -lcrypto iot.o tun2iot.o -o tun2iot
iot.o: In function `_iot_wfd_new':
iot.c:(.text+0x21b): undefined reference to `CRYPTO_malloc'
iot.o: In function `_iot_wfd_free':
iot.c:(.text+0x289): undefined reference to `CRYPTO_free'
iot.c:(.text+0x29d): undefined reference to `CRYPTO_free'
iot.o: In function `_iot_frame_ind':
iot.c:(.text+0x366): undefined reference to `CRYPTO_free'
iot.o: In function `_iot_error_ind':
iot.c:(.text+0x3f4): undefined reference to `CRYPTO_free'
iot.o: In function `_iot_do_phase_connect':
iot.c:(.text+0xb95): undefined reference to `SSL_connect'
iot.c:(.text+0xbad): undefined reference to `SSL_get_error'
iot.o: In function `_iot_frame_recv':
iot.c:(.text+0xfe6): undefined reference to `CRYPTO_free'
iot.c:(.text+0x1377): undefined reference to `CRYPTO_free'
iot.o: In …Run Code Online (Sandbox Code Playgroud) 编译时我遇到了一些错误,我无法弄清楚为什么......我的heapsort.h应该有导出类型吗?
heapsort.c
#include <stdio.h> // standard libraries already included in "list.h"
#include <stdlib.h>
#include "heap.h"
#include "heapsort.h"
void heapSort(int* keys, int numKeys){
heapHndl H = NULL;
H = buildHeap(numKeys, keys, numKeys);
for (int i = 1; i < numKeys; i++){
keys[i] = maxValue(H);
deleteMax(H);
}
freeHeap(&H);
}
Run Code Online (Sandbox Code Playgroud)
heapsort.h:
#ifndef _HEAPSORT_H_INCLUDE_
#define _HEAPSORT_H_INCLUDE_
#include <stdio.h>
#include <stdlib.h>
void heapSort(int* keys, int numKeys);
#endif
Run Code Online (Sandbox Code Playgroud)
当我使用我的客户端程序编译时,我在编译时遇到此错误:
HeapClient.o: In function `main':
HeapClient.c:(.text.startup+0x1a3): undefined reference to `heapsort'"
Run Code Online (Sandbox Code Playgroud) 我的印象是,您可以在一个文件中定义类的成员函数,然后在另一个文件中使用这些函数,只要这两个文件都被编译并发送到链接器即可.但是,如果我使用g ++(4.6.4),这样做会给我一个未定义的引用错误.有趣的是,使用intel编译器(icpc 11.0)不会出错,一切正常.是否有一些我可以用g ++设置的标志来使这个工作,或者是英特尔编译器让我逃避我不应该做的事情?以下是一些可以重现我的问题的代码:
class.h:
#ifndef _H
#define _H
typedef class
{
public:
int a;
int b;
void set(int x, int y);
int add(void);
} Test;
#endif
Run Code Online (Sandbox Code Playgroud)
class.cpp:
#include "class.h"
void Test::set(int x, int y)
{
a = x;
b = y;
}
int Test::add(void)
{
return a+b;
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中:
#include <cstdio>
#include "class.h"
int main(void)
{
Test n;
n.set(3, 4);
printf("%d\n", n.add());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为了编译,我做:
$ g++ class.cpp main.cpp -o test
/tmp/ccRxOI40.o: In function `main':
main.cpp:(.text+0x1a): undefined reference …Run Code Online (Sandbox Code Playgroud) 我在 GNU/Linux Debian 8.5 下编码
我有一个简单的程序。
如果我用gcc prog.c它编译它就可以了!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
int main(int argc, char const *argv[]) {
float _f = 3.1415f;
floor(_f);
ceil(_f);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我添加pow(),它会说它找不到pow,我需要添加gcc prog.c -lm以使其正确。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
int main(int argc, char const *argv[]) {
float _f = 3.1415f;
floor(_f);
ceil(_f);
pow(_f, 2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我是对的,pow(), ceil(),floor()都是来自<math.h>?
那么,为什么不floor() …