我正在开发一个包含大约30个源文件(.c)的C项目.我正在使用GNU工具在Ubuntu上运行的32位微控制器(i.MX515)上构建这个项目.
编译阶段成功完成,但是当链接过程开始时,我收到此错误(对于问题结尾处的完整错误):
In function `_start': init.c:(.text+0x30): undefined reference to `main'
Run Code Online (Sandbox Code Playgroud)
我有main()一个简单的功能printf().
我的Makefile链接,看起来像这样.
final: $(OBJDIR)/main.o $(OBJDIR)/TchClaKnn_BuildKdtreeInt.o $(OBJDIR)/TchClaKnn_FreeKdtreeInt.o.... (Go upto 30 files like this)
@echo ".Linking"
$(CC) $(LFLAGS) -o $(OBJDIR)/main.o $(OBJDIR)/TchClaKnn_BuildKdtreeInt.o $(OBJDIR)/TchClaKnn_FreeKdtreeInt.o..... (Go upto 30 files like this)
Run Code Online (Sandbox Code Playgroud)
救命!!!
问候
维克拉姆
完成链接错误
/usr/lib/gcc/arm-linux-gnueabi/4.3.3/../../../crt1.o: In function `_start':
init.c:(.text+0x30): undefined reference to `main'
collect2: ld returned 1 exit status
make[1]: *** [final] Error 1
make[1]: Leaving directory `/home/ubuntu/Documents/Project/IMX_Project_v1'
make: *** [all] Error 2
Run Code Online (Sandbox Code Playgroud) 我正在使用Linux,我有以下文件:
main.c, main.h
fileA.c, fileA.h
fileB.cpp, fileB.h
Run Code Online (Sandbox Code Playgroud)
该函数F1()在中声明fileB.h并定义fileB.cpp.我需要使用函数fileA.c,所以我将函数声明为
extern void F1();
Run Code Online (Sandbox Code Playgroud)
在fileA.c.
但是,在编译期间,我收到了错误
fileA.c: (.text+0x2b7): undefined reference to `F1'
Run Code Online (Sandbox Code Playgroud)
怎么了?
谢谢.
ETA:感谢我收到的答案,我现在有以下内容:
在fileA.h中,我有
#include fileB.h
#include main.h
#ifdef __cplusplus
extern "C"
#endif
void F1();
Run Code Online (Sandbox Code Playgroud)
在fileA.c中,我有
#include fileA.h
Run Code Online (Sandbox Code Playgroud)
在fileB.h中,我有
extern "C" void F1();
Run Code Online (Sandbox Code Playgroud)
在fileB.cpp中,我有
#include "fileB.h"
extern "C" void F1()
{ }
Run Code Online (Sandbox Code Playgroud)
但是,我现在有错误
fileB.h: error: expected identifier or '(' before string constant
Run Code Online (Sandbox Code Playgroud) 有人能告诉我这意味着什么吗?
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [program] Error 1
Run Code Online (Sandbox Code Playgroud)
我的make文件如下所示:
program : main.o render.o screenwriter.o
g++ -o main.o render.o screenwriter.o -lSDL
main.o : main.cpp render.h screenwriter.h
g++ -c main.cpp render.h screenwriter.h -lSDL
render.o : render.h render.cpp
g++ -c render.h render.cpp -lSDL
screenwriter.o : screenwriter.h screenwriter.cpp
g++ -c screenwriter.h screenwriter.cpp -lSDL
clean:
rm program main.o render.o screenwriter.o -lSDL
Run Code Online (Sandbox Code Playgroud)
谢谢.
面对一个可能稍微复杂的问题,解释和理解,因为给出整个画面会太大而且困难.
请原谅我.
考虑以下Makefile:
all: clients.so simulator backup
LD_PRELOAD=/home/Juggler/client/clients.so ./simulator
backup: backup.c libclient.a
gcc backup.c -o backup -L /home/Juggler/client -L. -lclient -ldl
simulator: simulator.c libclient.a
gcc -g simulator.c -o simulator -L /home/Juggler/client -L. -lclient -ldl -pthread
libclient.a: libclient.o client.o
ar rcs libclient.a libclient.o client.o
libclient.o:libclient.c
gcc -c libclient.c -o libclient.o -pthread
clients.so: client.o client_invoke.o
ld -shared -o clients.so client_invoke.o client.o -ldl
client_invoke.o: client_invoke.c
gcc -Wall -fPIC -DPIC -c -g client_invoke.c
client.o: client.c
gcc -Wall -fPIC -DPIC -c -g client.c -ldl -pthread …Run Code Online (Sandbox Code Playgroud) 我正在尝试编译依赖于Xerces XML Parser的项目.该项目没有任何困难地为Windows编译,但我在使用Cygwin中的g ++编译时遇到了一些麻烦.
为了使用Xerces,我试图针对静态库编译我的代码libxerces-c.a.但是当我这样做时,我会得到如下错误:
/tmp/cc2QGvMh.o:test.cpp:(.text+0x3a): undefined reference to `xercesc_2_8::DOMImplementationRegistry::getDOMImplementation(unsigned short const*)'
Run Code Online (Sandbox Code Playgroud)
我已经检查过静态库ar,并确认它包含DOMImplementationRegistry.o定义我正在调用的函数的文件.
ar -t libxerces-c.a
...
DOMImplementationImpl.o
DOMImplementationRegistry.o
DOMLocatorImpl.o
...
Run Code Online (Sandbox Code Playgroud)
我还从库中提取了目标文件,并使用'nm'来确保我调用的函数实际存在:
ar -x libxerces-c.a
nm --demangle DOMImplementationRegistry.o
...
00000080 T xercesc_2_8::getDOMImplSrcVectorMutex()
00000300 T xercesc_2_8::DOMImplementationRegistry::getDOMImplementation(unsigned short const*)
000002a0 T xercesc_2_8::DOMImplementationRegistry::addSource(xercesc_2_8::DOMImplementationSource*)
...
Run Code Online (Sandbox Code Playgroud)
由于我可以为Windows编译所有内容但不能用g ++编译,我认为错误可能在链接器顺序中(类似于此问题中描述的问题).但是,即使更改了链接器顺序,我仍然会得到相同的编译器错误.我试过了两个
g++ -o test.exe test.cpp -Llib -lxerces-c
Run Code Online (Sandbox Code Playgroud)
和
g++ -o test.exe test.cpp lib/libxerces-c.a
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我试图做一个继承项目,我收到这个错误:
/tmp/ccw1aT69.o: In function `main':
main.cpp:(.text+0x15): undefined reference to `Derived::Derived(int)'
/tmp/ccw1aT69.o: In function `Derived::~Derived()':
main.cpp:(.text._ZN20DerivedD2Ev[_ZN20DerivedD5Ev]+0x13): undefined reference to `vtable for Derived'
main.cpp:(.text._ZN20DerivedD2Ev[_ZN20DerivedD5Ev]+0x1f): undefined reference to `Base::~Base()'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
main.cpp中:
#include <iostream>
#include "Base.h"
#include "Derived.h"
int main() {
Derived intList(25);
}
Run Code Online (Sandbox Code Playgroud)
base.h:
#ifndef BASE_H
#define BASE_H
class Base {
public:
...
Base (const Base& otherList);
virtual ~Base();
protected:
int *list;
int length;
int maxSize;
};
#endif
Run Code Online (Sandbox Code Playgroud)
Base.cpp:
#include "Base.h"
#include …Run Code Online (Sandbox Code Playgroud) 我必须为学校项目建立一个图书馆,并以较小的用途使用该图书馆。现在,我已经制作了XPM_lib.h和XPM_lib.c文件以及我的test.c文件。但是,当我尝试编译test.c文件时,出现“对initXPM_image的未定义引用”错误。
我的文件是(XPM_lib.h):
#ifndef LIBRARYXPM_H_INCLUDED
#define LIBRARYXPM_H_INCLUDED
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define HEADER "/*XPM*/"
#define STRING_BEGIN "static char *egc[] = {\n\n /*width , height , ncolors , charsperpixel */ \n "
#define STRING_END "\n }"
#define STRING_SEPARATOR ","
#define STRING_COLORS "/* colors #RRGGBB */"
#define STRING_PIXELS "/* pixels */"
struct Color{
unsigned char r,g,b;
char *color_char;
char key[2];
};
struct XPM_image {
unsigned int width;
unsigned int height;
unsigned int no_colors; // number of colors used
unsigned char char_per_pixel; // number of …Run Code Online (Sandbox Code Playgroud) 我以前写过复杂的C和C++ makefile.但是,我似乎无法让我的D makefile工作.它抛出超过一千行"未定义的引用"错误,看起来好像Phobos无法链接.我该如何解决这个问题?
我在Fedora 19 Linux上使用GNU make和LDC2.
编辑:使用LDC2直接编译和链接正常工作.只有在使用'make'调用时才会出错.make似乎试图调用一个单独的链接器.
编辑2:这是我的makefile:
# This macro contains the source files
sources := $(wildcard *.d)
binaries := $(sources:%.d=%)
all: $(binaries)
%.o:%.d
ldc2 $< -O5 -check-printf-calls
Run Code Online (Sandbox Code Playgroud)
删除.o修复它.
我正在尝试修复memcpy_s()错误的未定义引用。我已经将其包含string.h在文件中,并且该memcpy()功能可以正常运行,并且我还尝试了include memory.h。我在x64 Windows 7上并使用gcc 4.8.1进行编译。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void doMemCopy(char* buf, size_t buf_size, char* in, int chr) {
memcpy_s(buf, buf_size, in, chr);
}
Run Code Online (Sandbox Code Playgroud)
buf已在主函数调用中分配了的内存doMemCpy(buf, 64, in, bytes)。in是从标准输入读取的字符串
来自cmd终端的确切错误:
未定义对“ memcpy_s” collect2.exe的引用:错误:ld返回1退出状态
我是在Linux上编写程序的新手.我有一个使用一个单独的模块程序shm_open,ftruncate,mmap,fork,和wait.我编译了这个程序,gcc -c然后将其链接ld -lrt(需要librt shm_open),我得到一个奇怪的链接器错误:
undefined reference to symbol 'waitpid@@GLIBC_2.2.5'
Run Code Online (Sandbox Code Playgroud)
该联机帮助wait表示
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
waitid():
Since glibc 2.26: _XOPEN_SOURCE >= 500 ||
_POSIX_C_SOURCE >= 200809L
Glibc 2.25 and earlier:
_XOPEN_SOURCE
Run Code Online (Sandbox Code Playgroud)
但#define _XOPEN_SOURCE如果我这样做,那么输入代码就无济于事了
gcc -c -D _XOPEN_SOURCE
Run Code Online (Sandbox Code Playgroud)
编译器说隐式声明ftruncate.
我在VMWare下运行Ubuntu.GCC是版本gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609.
可能有什么不对?