我有一个与Linker 在目标文件和静态库中共存相同符号时不会发出多个定义错误的问题非常密切相关,但涉及的情况略有不同。就像那里一样,我有两个 .cpp 文件(比如:test1.cpp和test2.cpp),每个文件都包含相同功能的实现,即void testfunc(). 我还有一个头文件test.h,我在其中声明testfunc,以及一个main.cpp带有 main 函数的文件,其中包含这样的调用testfunc():
include "test.h"
int main() {
testfunc();
}
Run Code Online (Sandbox Code Playgroud)
我通过调用分别编译 .cpp 文件g++ -c *.cpp,然后使用ar rvs libtest.a test1.o test2.o. 当链接main.o对库现在,链接器不会抱怨,因为我希望它做的事:
gcc main.o -L. -ltest -o main
Run Code Online (Sandbox Code Playgroud)
生成的可执行文件工作得非常好 - 调用testfunc(). 老实说,我预计multiple definition of...会发生一些错误。因此,我的问题是:
ar,它只将两个目标文件中的一个添加到库中,或者库是否包含两个目标文件,这种行为的原因是在链接过程中找到的,其中链接器在找到testfunc? 的一个定义后停止搜索库。testfunc实际使用的定义还是这个定义?即,它可能是ar决定使用哪个参数的顺序?ar或者这可能取决于系统?我试图链接一个将两个归档库组合到一个C程序的精简归档.
我构建了两个简单的hello world函数,并使用以下命令构建了一个归档文件:
ar rcs lib1.a lib1.o
ar rcs lib2.a lib2.o
Run Code Online (Sandbox Code Playgroud)
然后使用精简归档合并两个归档:
ar rcsT all_lib.a lib1.a lib2.a
Run Code Online (Sandbox Code Playgroud)
然后用gcc编译:
gcc main.o all_lib.a -o hello
Run Code Online (Sandbox Code Playgroud)
我最终收到一条错误消息:
ld:警告:忽略文件all_lib.a,文件是为不支持的文件格式构建的,而不是链接的体系结构(x86_64)
体系结构x86_64的未定义符号:"_ func1",引用自:main.o"_func2"中的_main,引自:main.o中的_main ld:未找到体系结构x86_64的符号
如果我尝试直接将main.o与lib1.a和lib2.a链接起来,一切正常.
我在Mac OSX 10.6.8上使用gcc(MacPorts gcc46 4.6.3_3)4.6.3和GNU ar(GNU Binutils)2.21.
Makefile文件
test1: main.o lib1.o lib2.o
gcc main.o lib1.a lib2.a -o hello
test2: main.o combine
gcc main.o all_lib.a -o hello
lib1.o: lib1.c
gcc -c lib1.c
ar rcs lib1.a lib1.o
lib2.o: lib2.c
gcc -c lib2.c
ar rcs lib2.a lib2.o
combine: lib1.o lib2.o
ar rcsT all_lib.a lib1.a …Run Code Online (Sandbox Code Playgroud) 免责声明:这是一项任务.我不是要求明确的代码.相反,我只是要求足够的帮助,我可以理解我的问题,并自己纠正.
我正在尝试ar根据家庭作业重新创建Unix 实用程序.这个任务的大部分处理C中的文件IO,其他部分处理系统调用等.
在这种情况下,我打算创建存档中所有文件的简单列表.你可能没注意到,我还没有走得太远.该计划相对简单:从存档文件中读取每个文件头,并仅打印保存的值ar_hdr.ar_name.其余字段将被跳过fseek(),包括文件数据,直到到达另一个文件,此时该过程再次开始.如果达到EOF,则该功能简单地终止.
我对文件IO没什么经验,所以我已经处于这个任务的劣势.我已尽最大努力研究实现目标的正确方法,我相信我已尽最大努力实施这些目标.也就是说,我的实施似乎有问题.来自存档文件的数据似乎没有被读取,或者至少存储为变量.这是我的代码:
struct ar_hdr
{
char ar_name[16]; /* name */
char ar_date[12]; /* modification time */
char ar_uid[6]; /* user id */
char ar_gid[6]; /* group id */
char ar_mode[8]; /* octal file permissions */
char ar_size[10]; /* size in bytes */
};
void table()
{
FILE *stream;
char str[sizeof(struct ar_hdr)];
struct ar_hdr temp;
stream = fopen("archive.txt", "r");
if (stream == 0)
{
perror("error");
exit(0);
}
while (fgets(str, sizeof(str), stream) != …Run Code Online (Sandbox Code Playgroud) 我需要访问一个ar创建的文件,但我找不到定义格式的规范文档.有人能指出我正确的方向吗?
我有这个makefile
libjackpot.a: jackport.o jackpot.o
ar -rcs jackport.o jackpot.o
jackpot.o: jackpot.cpp jackpot.h
g++ jackpot.cpp -std=c++11 -O2 -c
jackport.o: jackport.cpp jackpot.h jackport.h
g++ jackport.cpp -std=c++11 -O2 -c
Run Code Online (Sandbox Code Playgroud)
不知何故(在我的Linux机器上),我明白了
ar: jackport.o: File format not recognized
Run Code Online (Sandbox Code Playgroud)
ar - 帮助给出了
ar: supported targets: elf64-x86-64 elf32-i386 elf32-x86-64 a.out-i386-linux pei-i386 pei-x86-64 elf64-l1om elf64-k1om elf64-little elf64-big elf32-little elf32-big plugin srec symbolsrec verilog tekhex binary ihex
Run Code Online (Sandbox Code Playgroud)
文件jackport.o
jackport.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
Run Code Online (Sandbox Code Playgroud) 我正在尝试生成一个静态库并将其与执行二进制文件链接.
这是一个库函数:
#include <stdio.h>
int hello() {
return 10;
}
Run Code Online (Sandbox Code Playgroud)
使用这些命令,我可以得到一个静态库.
gcc -c io.c
ar -crv libio.a io.o
Run Code Online (Sandbox Code Playgroud)
有了lip -info,我检查了它是x86_64架构.
ar> lipo -info libio.a
input file libio.a is not a fat file
Non-fat file: libio.a is architecture: x86_64
Run Code Online (Sandbox Code Playgroud)
这是使用库的主要功能.
#include <stdio.h>
extern int hello();
int main(int argc, char *argv[]) {
printf("%d", hello());
}
Run Code Online (Sandbox Code Playgroud)
但是,当我将对象与静态库链接时,我有错误.
gcc main.c -lio -o main -L.
Run Code Online (Sandbox Code Playgroud)
错误消息是:
ld: warning: ignoring file ./libio.a, file was built for archive which is not the architecture …Run Code Online (Sandbox Code Playgroud) 我正在 OS X 上进行测试。我们有一个configure.ac和Makefile.am. 自动工具选择了错误AR,并ARFLAGS为平台。它发生在(和没有)AM_PROG_ARin Makefile.am:
$ egrep 'AR =|ARFLAGS =' Makefile
AMTAR = $${TAR-tar}
AR = ar
ac_ct_AR = ar
Run Code Online (Sandbox Code Playgroud)
Autoconf 应该使用 Apple 的libtool(不要与 Autotools' 混淆libtool)和libtool's 标志。Applelibtool正确处理胖库和交叉编译。它应该是这样的:
AR = /usr/bin/libtool
ARFLAGS = -static -o
Run Code Online (Sandbox Code Playgroud)
Apple 的Porting UNIX/Linux Applications to OS X没有讨论这个话题,我在 Autoconf 文档中也找不到它。Autoconf 文档也缺少一个AC_PROG_AR(或类似的)。请参阅Autoconf 手册中的5.2.1 特定程序检查。
我们如何告诉 Autoconf 使用 Apple 的平台构建工具,而不是 Linux …
我尝试创建一个库并对其进行测试,但发生了错误。
错误代码:
./libasm.a: error adding symbols: Archive has no index; run ranlib to add one
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
我是这样编译的。
nasm -f macho64 ft_strlen.s -o ft_strlen.o
ar rcs libasm.a ft_strlen.o
ranlib libasm.a
gcc main.c libasm.a
下面是源文件
;ft_strlen.s
segment .text
global ft_strlen
ft_strlen:
mov rax, 0
jmp count
count:
cmp BYTE [rdi + rax], 0
je exit
inc rax
jmp count
exit:
ret
Run Code Online (Sandbox Code Playgroud)
./libasm.a: error adding symbols: Archive has no index; run ranlib to add one
collect2: error: …Run Code Online (Sandbox Code Playgroud) 我知道当链接到多个静态库或目标文件时,顺序很重要(依赖库应该在它们的依赖项之前列出).我想知道,在创建库文件时ar,是否应用此相同规则并且库中的顺序很重要,或者在同一.a文件中它是否有所作为.
我正在打包200多个带有复杂依赖关系图的目标文件,并且这样做
ar rcs mylib.a objs/*.o
Run Code Online (Sandbox Code Playgroud)
然后以正确的顺序列出它们要容易得多.
我正在使用gcc,如果它有所作为.
我有一个简单的cmake项目,我无法在OS X 10.8.4上编译.cmake/make过程在Linux上运行良好但在OS XI上遇到此错误:
Linking CXX static library libImageFilter.a
ar: no archive members specified
...
make[2]: *** [lib/libImageFilter.a] Error 1
make[1]: *** [lib/CMakeFiles/ImageFilter.dir/all] Error 2
make: *** [all] Error 2
Run Code Online (Sandbox Code Playgroud)
我在两个平台上都使用Eclipse CDT4 Generator Unix MakeFile.这似乎与两个系统上的ar之间的差异有关.但是,我在谷歌找不到太多帮助我排除故障.
这里有一些更多的信息
SRC/CMakeList.txt
make_minimum_required(VERSION 2.8)
project(itkNormals)
FIND_PACKAGE (ITK REQUIRED)
IF( ITK_FOUND )
include( ${ITK_USE_FILE} )
ENDIF( ITK_FOUND )
add_subdirectory(test)
add_subdirectory(lib)
Run Code Online (Sandbox Code Playgroud)
SRC/LIB/CMakeList.txt
add_library(DotImageFilter itkDotImageFilter.h)
SET_TARGET_PROPERTIES(DotImageFilter PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(DotImageFilter ${ITK_LIBRARIES})
add_library(ImageFilter itkImageFilter.hxx)
SET_TARGET_PROPERTIES(ImageFilter PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(ImageFilter ${ITK_LIBRARIES})
Run Code Online (Sandbox Code Playgroud)
的src /测试/的CMakeLists.txt:
include_directories(${PROJECT_SOURCE_DIR}/lib)
add_executable(itkNormalsMain itkNormals.cxx)
TARGET_LINK_LIBRARIES(itkNormalsMain ${ITK_LIBRARIES})
TARGET_LINK_LIBRARIES(itkNormalsMain ImageFilter) …Run Code Online (Sandbox Code Playgroud)