这真的是一个新手问题.我正在学习C,我不明白如何将不同的文件链接在一起.
我有一个标题
/* file2.h */
int increment(int x);
Run Code Online (Sandbox Code Playgroud)
和一个C文件
/* file2.c */
#include "file2.h"
int increment(int x)
{
return x+1;
}
Run Code Online (Sandbox Code Playgroud)
现在我想要包含标题file1.c以便使用该函数increment.根据我的理解,我必须做以下事情:
/* file1.c*/
#include "file2.h"
int main()
{
int y = increment(1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试编译整个事情时,使用
gcc -o program file1.c
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息
/tmp/ccDdiWyO.o: In function `main':
file1.c:(.text+0xe): undefined reference to `increment'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
但是,如果我还包括file2.c
/* file1.c*/
#include "file2.h"
#include "file2.c" /* <--- here it is! */
int main()
{
int y = …Run Code Online (Sandbox Code Playgroud) 我正在尝试编译这个名为Pirni的C程序,它最初是为iPhone制作的,我想在Linux Ubuntu 12.04 LTS上运行它.它很短,(3个C文件和1个头文件).但我得到以下错误.我确实libpcap-dev安装了,但我确实包含了-lpcap所以我不知道可能出现什么问题.
gcc -Wall -pthread -lpcap pirni.c threads.c sniffer.c -o pirni -lnet
pirni.c: In function ‘set_forwarding’:
pirni.c:31:2: warning: implicit declaration of function ‘sysctlbyname’ [-Wimplicit-function-declaration]
/tmp/cc3sf284.o: In function `set_forwarding':
pirni.c:(.text+0xfd): undefined reference to `sysctlbyname'
/tmp/ccvGjyMo.o: In function `processPacket':
sniffer.c:(.text+0x1b): undefined reference to `pcap_dump'
/tmp/ccvGjyMo.o: In function `initSniffer':
sniffer.c:(.text+0xfa): undefined reference to `pcap_open_live'
sniffer.c:(.text+0x15a): undefined reference to `pcap_lookupnet'
sniffer.c:(.text+0x1af): undefined reference to `pcap_compile'
sniffer.c:(.text+0x1dd): undefined reference to `pcap_setfilter'
sniffer.c:(.text+0x222): undefined reference to `pcap_dump_open'
sniffer.c:(.text+0x27c): …Run Code Online (Sandbox Code Playgroud) 所以我有三个文件:
jarvismarch.c
jarvismarchtools.c
jarvismarchtools.h
Run Code Online (Sandbox Code Playgroud)
运行后make,我收到以下错误:
Andrew-Carpenters-Laptop:Independent Study ahcarpenter$ make
cc -c -o jarvismarch.o jarvismarch.c
cc -c -o jarvismarchtools.o jarvismarchtools.c
cc -o jarvismarch jarvismarch.o jarvismarchtools.o
ld: duplicate symbol _string1 in jarvismarchtools.o and jarvismarch.o for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [all] Error 1
Run Code Online (Sandbox Code Playgroud)
string 1在...内宣布jarvismarchtools.c.jarvismarchtools.h包括在内jarvismarchtools.c以及jarvismarch.c.
关于如何解决此错误的任何想法?
内jarvismarchtools.h:
/*
FILENAME: jarvismarchtools.h
AUTHOR: Andrew H. Carpenter
DATE: 2 …Run Code Online (Sandbox Code Playgroud) 基本上我试图让SFML和Box2D在VS2012下一起工作.我让他们两个独立工作,但我不能让他们一起工作.
如果我尝试一起运行它们,我会得到一堆:
error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MDd_DynamicDebug' doesn't match value 'MTd_StaticDebug' in Main.obj
Run Code Online (Sandbox Code Playgroud)
如果我将属性中的RuntimeLibrary值从MDd更改为MTd,则会出现一堆错误,例如:
error LNK2005: __amsg_exit already defined in MSVCRTD.lib(MSVCR110D.dll)
error LNK2005: __cexit already defined in MSVCRTD.lib(MSVCR110D.dll)
error LNK2005: __exit already defined in MSVCRTD.lib(MSVCR110D.dll)
Run Code Online (Sandbox Code Playgroud)
一堆那些以及两个警告:
warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library`
warning LNK4098: defaultlib 'libcmtd.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
Run Code Online (Sandbox Code Playgroud)
如果我也不工作,我得到一堆类似的错误:
error LNK2001: unresolved external symbol __purecall
对于SFML,我非常肯定我正确地构建了它.另一方面,对于Box2D,我不太确定.我使用了本指南,你可以看到它适用于VS2010.但是我在构建和HelloWorld.cpp示例时没有遇到任何问题.
那么我可以做些什么来完成这项工作?
编辑:我已经让它工作,但只有调试版本我做的是我尝试重建Box2D库作为/ MTd进行调试和/ MT发布调试一个内置正常,而发布版本没有.
调试版本工作正常,但发布版本抛出
warning …Run Code Online (Sandbox Code Playgroud) 我正在尝试将AdMob应用到我的iOS应用中.我按照他们在这里提供的文档.所以我尝试构建并运行,我收到以下错误:
Undefined symbols for architecture i386:
"_NSOverwriteMergePolicy", referenced from:
anon in libGoogleAnalyticsServices.a(GAIDataStore.o)
anon in libGoogleAnalyticsServices.a(TAGModel.o)
anon in libGoogleAnalyticsServices.a(GAIDataStore.o)
anon in libGoogleAnalyticsServices.a(TAGModel.o)
"_NSSQLiteErrorDomain", referenced from:
anon in libGoogleAnalyticsServices.a(GAIDataStore.o)
"_NSSQLiteStoreType", referenced from:
anon in libGoogleAnalyticsServices.a(GAIDataStore.o)
anon in libGoogleAnalyticsServices.a(TAGModel.o)
anon in libGoogleAnalyticsServices.a(GAIDataStore.o)
anon in libGoogleAnalyticsServices.a(TAGModel.o)
"_OBJC_CLASS_$_MFMailComposeViewController", referenced from:
objc-class-ref in libGoogleAdMobAds.a(GADOpener.o)
"_OBJC_CLASS_$_MFMessageComposeViewController", referenced from:
objc-class-ref in libGoogleAdMobAds.a(GADOpener.o)
"_OBJC_CLASS_$_NSAttributeDescription", referenced from:
objc-class-ref in libGoogleAnalyticsServices.a(GAICoreDataUtil.o)
objc-class-ref in libGoogleAnalyticsServices.a(TAGModel.o)
"_OBJC_CLASS_$_NSEntityDescription", referenced from:
objc-class-ref in libGoogleAnalyticsServices.a(GAIDataStore.o)
objc-class-ref in libGoogleAnalyticsServices.a(GAICoreDataUtil.o)
objc-class-ref in libGoogleAnalyticsServices.a(TAGDataProvider.o)
objc-class-ref in libGoogleAnalyticsServices.a(TAGModel.o) …Run Code Online (Sandbox Code Playgroud) 在尝试构建我的项目时,我不断得到这个错误,说ld: 11 duplicate symbols for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我最近尝试将GPUImage实现到我的项目中,这与它有什么关系吗?我也读过类似的问题,但没有人有我的解决方案.
我正在开发一个OpenCV 3.0.0 32位分段项目,并从OpenCV 复制+粘贴一些示例代码.不幸的是,当我构建它时,包含所有依赖项和库,它会给我以下错误:
1>Source.cpp
1>Source.obj : error LNK2019: unresolved external symbol "public: __thiscall cv::VideoCapture::VideoCapture(void)" (??0VideoCapture@cv@@QAE@XZ) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall cv::VideoCapture::~VideoCapture(void)" (??1VideoCapture@cv@@UAE@XZ) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "public: virtual bool __thiscall cv::VideoCapture::open(class cv::String const &)" (?open@VideoCapture@cv@@UAE_NABVString@2@@Z) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "public: virtual bool __thiscall cv::VideoCapture::isOpened(void)const " (?isOpened@VideoCapture@cv@@UBE_NXZ) referenced in function _main
Run Code Online (Sandbox Code Playgroud) 场景:
在Netbeans IDE中创建的AC应用程序,包含以下两个文件:
some_function.c
#include <stdio.h>
int function_1(int a, int b){
printf("Entered Value is = %d & %d\n",a,b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
newmain.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
//function_2(); //Error //function name not allowed
function_1();
function_1(1);
function_1(1,2);
return (EXIT_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)
当在C程序中学习头文件的需要时,我尝试了上述应用程序(原样).它被编译并给出如下输出
输入值= 4200800&102
输入值= 1和102
输入值= 1和2
问题1 :(我意识到,在开始阶段,理解链接器程序的过程很难,因此我问这个问题.)我的假设是正确的,当链接时,"链接器将检查函数名称而不是参数"在没有使用头文件的情况下?
关于头文件的使用,我遇到了这个链接,它说,我们可以使用#include包含C文件本身.所以,我在newmain.c文件中使用了以下行
#include "some_function.c"
Run Code Online (Sandbox Code Playgroud)
正如所料,它显示了以下错误
错误:函数'function_1()'
错误的参数太少:函数'function_1(1)'的参数太少
而且,我得到了以下(意外)错误:
some_function.c:8:`function_1'some_function.c:8的多重定义
:首先在这里定义
问题2:包含'c'文件本身时我做了什么错误,因为它给出了上述(意外)错误?
我有一个visual studio 2012 c ++项目.我最近卸载了它并安装了visual studio 2015并升级了该项目.当我构建项目时,收到错误,如下所示:
错误LNK2019未解析函数中引用的外部符号_memcmp
此外,我没有在我的代码memcmp fucntion中使用任何地方.
我使用了链接器详细功能,可以在输出文件中看到:
找到_memcmp
在MyC++ Project.obj中引用
在libcpmtd.lib(xstrcoll.obj)中引用
加载libvcruntimed.lib(__ memcmp_.obj)
这里有两个问题
1.即使我没有在我的代码中使用memcmp为什么我收到链接器错误?
2.为什么memcmp被加载为__memcmp_.obj
我的项目中也有以下设置:
1.C++ - >代码生成 - >运行时库设置为/ MTd
2.Linker - >忽略所有默认库都设置为空
我已经尝试了所有的项目设置,但一切都是徒劳的.
我只发布了这个我没有用过的memcmp函数.
我使用过mamcpy和memset而没有问题
我正在尝试创建一个静态库的静态库。这是我的CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(myRtspClient)
add_subdirectory(../third_party/Base64_live555 base64_live555)
add_subdirectory(../third_party/md5 md5)
add_subdirectory(../third_party/JRTPLIB jrtplib)
include_directories(include)
include_directories(../third_party/Base64_live555/include)
include_directories(../third_party/md5/include)
include_directories(jrtplib/src)
include_directories(../third_party/JRTPLIB/src)
file(GLOB SOURCES "*.cpp")
add_library(myRtspClient STATIC ${SOURCES})
add_library(libmd5 STATIC IMPORTED)
SET_PROPERTY(TARGET libmd5 PROPERTY IMPORTED_LOCATION ./md5/libmd5.a)
add_library(libbase64_live555 STATIC IMPORTED)
SET_PROPERTY(TARGET libbase64_live555 PROPERTY IMPORTED_LOCATION ./base64_live555/libbase64_live555.a)
add_library(libjrtp STATIC IMPORTED)
SET_PROPERTY(TARGET libjrtp PROPERTY IMPORTED_LOCATION ./jrtplib/src/librtp.a)
target_link_libraries(myRtspClient libmd5 libbase64_live555 libjrtp)
#install(TARGETS myRtspClient DESTINATION /usr/lib)
Run Code Online (Sandbox Code Playgroud)
如果您想查看整个图片:https : //github.com/lucaszanella/myRtspClient/blob/8658dbcb8ed071b8d2649a471455f57f268932f4/myRtspClient/CMakeLists.txt
如您所见,我正在尝试myRtspClient通过与链接来创建目标libmd5 libbase64_live555 libjrtp。由于cmake没有错误,即使我这样做
target_link_libraries(myRtspClient eewgg dsgsg dgsgsdgsg)
Run Code Online (Sandbox Code Playgroud)
我不确定这是什么错误。这些库在我指定的位置。但是,我不知道它们是否在第一次编译中。虽然我尝试了第二次,但是谁知道...
因此,继续...我尝试了很多SET_PROPERTY类似的方法:
SET_PROPERTY(TARGET libbase64_live555 PROPERTY IMPORTED_LOCATION ./base64_live555/libbase64_live555.a) …Run Code Online (Sandbox Code Playgroud)