我尝试将包含.a文件的位置添加到LDFLAGS,然后执行AC_CHECK_LIB,但找不到.也许我的语法是错误的,或者我缺少一些更基本的东西,这并不奇怪,因为我是一个新手,也不知道自己在做什么.这是我尝试过的:AC_CHECK_HEADER([$location/helper.h], [AC_DEFINE([HAVE_HELPER_H], [1], [found helper.h]) CFLAGS="$CFLAGS -I$location"; LDFLAGS="$LDFLAGS -L$location"; AC_CHECK_LIB(helper)])没有骰子. AC_CHECK_LIB正在寻找-lhelper我猜(或libhelper吗?),所以我不确定这是否是问题,所以我也尝试了这一点(忽略AC_CHECK_LIB并直接在LDFLAGS中包含.a),但是没有运气:AC_CHECK_HEADER([$location/helper.h], [AC_DEFINE([HAVE_HELPER_H], [1], [found helper.h]) CFLAGS="$CFLAGS -I$location"; LDFLAGS="$LDFLAGS -L$location/helper.a"])要模拟手动编译,我尝试删除-L,但这无济于事:AC_CHECK_HEADER([$location/helper.h], [AC_DEFINE([HAVE_HELPER_H], [1], [found helper.h]) CFLAGS="$CFLAGS -I$location"; LDFLAGS="$LDFLAGS $location/helper.a"])我尝试了其他组合和排列,但是我想我可能会缺少一些更基本的东西. ================更新我通过使用_LDADD来使用Makefile.am中的.a文件的硬编码路径来工作,myprog_LDADD=/home/john/mystuff/helper.a但是我无法预测.a文件的位置.由于某种原因,在configure.ac中定义myprog_LDADD不起作用(我希望这样做,所以我可以使用我的动态位置变量),并且似乎对LDFLAGS,myprog_LDFLAGS和AM_LDFLAGS的更改没有组合.如果在Makefile.am中,我尝试使用configure.ac中定义的变量位置,则它不起作用myprog_LDADD=($location)helper.a ================更新我想我已经解决了,但是由于我不知道自己在做什么,所以我非常感谢您的反馈.我使用AC_SUBST()从configure.ac中获取myprog_LDADD,因此最终的解决方案如下所示:AC_CHECK_HEADER([$location/helper.h], [AC_DEFINE([HAVE_HELPER_H], [1], [found helper.h]) CFLAGS="$CFLAGS -I$location" myprog_LDADD="$location/helper.a" AC_SUBST(myprog_LDADD)])解决方案您可以在configure.ac中设置位置:LOCATION=/home/john/mystuffAC_SUBST(LOCATION) AC_SUBST在所有Makefile.am中定义了变量$LOCATION,并且还将所有出现的@LOCATION@替换为$LOCATION的内容.因此,在您的Makefile.am中,您可以myprog_CPPFLAGS="-I$LOCATION"myprog_LDADD="$LOCATION/helper.a" PS.之所以需要直接引用该库,是因为-l在系统库目录中寻找一个正确命名的库(例如libhelper.a).但是,由于静态库和目标文件之间并没有太大的区别,因此不需要使用-l神奇地引用它.您可以像现在一样将其编译到程序中.I am trying to migrate my application from manual build to autoconf, which is working very nicely so far. But I have one static library that I can't figure out how to integrate. That library will NOT be located in the usual library locations - the location of the binary (.a file) and header (.h file) will be given as a configure argument. (Notably, even if I move the .a file to /usr/lib or anywhere else I can think of, it still won't work.) It is also not named traditionally (it does not start with "lib" or "l").Manual compilation is working with these (directory is not predictable - this is just an example):gcc ... -I/home/john/mystuff /home/john/mystuff/helper.a(Uh, I actually don't understand why the .a file is referenced directly, not with -L or anything. Yes, I have a half-baked understanding of building C programs.)So, in my configure.ac, I can use the relevant configure argument to successfully find the header (.h file) using AC_CHECK_HEADER. Inside the AC_CHECK_HEADER I then add the location to CPFLAGS and the #include of the header file in the actual C code picks it up nicely.Given a configure argument that has been put into $location and the name of the needed files are helper.h and helper.a (which are both in the same directory), here is what works so far:AC_CHECK_HEADER([$location/helper.h], [AC_DEFINE([HAVE_HELPER_H], [1], [found helper.h]) CFLAGS="$CFLAGS -I$location"])Where I run into difficulties is getting the binary (.a file) linked in. No matter what I try, I always get an error about undefined references to the function calls for that library. I'm pretty sure it's a linkage issue, because I can fuss with the C code and make an intentional error in the function calls to that library which produces earlier errors that indicate that the function prototypes have been loaded and used to compile.I tried adding the location that contains the .a file to LDFLAGS and then doing a AC_CHECK_LIB but it is not found.Maybe my syntax is wrong, or maybe I'm missing something more fundamental, which would not be surprising since I'm a newbie and don't really know what I'm doing.Here is what I have tried:AC_CHECK_HEADER([$location/helper.h], [AC_DEFINE([HAVE_HELPER_H], [1], [found helper.h]) CFLAGS="$CFLAGS -I$location"; LDFLAGS="$LDFLAGS -L$location"; AC_CHECK_LIB(helper)])No dice. AC_CHECK_LIB is looking for -lhelper I guess (or libhelper?) so I'm not sure if that's a problem, so I tried this, too (omit AC_CHECK_LIB and include the .a directly in LDFLAGS), without luck:AC_CHECK_HEADER([$location/helper.h], [AC_DEFINE([HAVE_HELPER_H], [1], [found helper.h]) CFLAGS="$CFLAGS -I$location"; LDFLAGS="$LDFLAGS -L$location/helper.a"])To emulate the manual compilation, I tried removing the -L but that doesn't help:AC_CHECK_HEADER([$location/helper.h], [AC_DEFINE([HAVE_HELPER_H], [1], [found helper.h]) CFLAGS="$CFLAGS -I$location"; LDFLAGS="$LDFLAGS $location/helper.a"])I tried other combinations and permutations, but I think I might be missing something more fundamental....================ UPDATEI got it to work with a hard-coded path to the .a file in Makefile.am using _LDADD like this:myprog_LDADD=/home/john/mystuff/helper.aBut I can't predict the location of the .a file. For some reason, defining myprog_LDADD in configure.ac doesn't work (I wish it did, so I can use my dynamic location variable), and no combination of changes to LDFLAGS, myprog_LDFLAGS, AM_LDFLAGS seems to work.If, in Makefile.am, I try to use the variable location that is defined in configure.ac, it doesn't workmyprog_LDADD=($location)helper.a================ UPDATEI think I figured it out, but since I have no idea what I'm doing, I'd REALLY appreciate some feedback. I used AC_SUBST() to get myprog_LDADD to work from configure.ac, so the final solution looks like this:AC_CHECK_HEADER([$location/helper.h], [AC_DEFINE([HAVE_HELPER_H], [1], [found helper.h]) CFLAGS="$CFLAGS -I$location" myprog_LDADD="$location/helper.a" AC_SUBST(myprog_LDADD)]) 解决方案 You can set the location in configure.ac:LOCATION=/home/john/mystuffAC_SUBST(LOCATION)AC_SUBST defines the variable $LOCATION in all your Makefile.ams and also replaces all occurrences of @LOCATION@ with the contents of $LOCATION. So then in your Makefile.am you can domyprog_CPPFLAGS="-I$LOCATION"myprog_LDADD="$LOCATION/helper.a"PS. The reason why you need to reference the library directly is because -l looks for a properly-named library (e.g. libhelper.a) in the system library directories. However, since there's not all that much difference between a static library and an object file, there's no need to magically reference it using -l; you can just compile it into your program like you are doing now. 这篇关于Autoconf-包括一个静态库(新手)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-26 14:04
查看更多