简体   繁体   English

在Linux上将自定义Apache 2.4模块与httpd / apr库静态链接时出现错误

[英]Errors when linking a custom Apache 2.4 modules statically with httpd/apr libraries on Linux

I am attempting to move my module to Linux Apache 2.4 and I am having linking issues. 我试图将我的模块移至Linux Apache 2.4,但出现链接问题。 On windows a libhttpd.lib is available to link against as well as the apr/apr-util libraries. 在Windows上,可以使用libhttpd.lib以及apr / apr-util库进行链接。 lib* httpd apr and aprutil are all statically linked on my windows installation. lib * httpd apr和aprutil在Windows安装中都是静态链接的。 I want to do the same for the Linux installation. 对于Linux安装,我也想这样做。

According to the limited documentation available I am unable to use APXS because my module is written in C++. 根据可用的有限文档,我无法使用APXS,因为我的模块是用C ++编写的。

I am having difficulties finding the archive files for the server on Linux. 我很难在Linux上找到服务器的存档文件。 What do I need to link against for my module to work? 我需要链接到什么才能使我的模块正常工作?

The source is able to link and execute on a Windows host. 源能够在Windows主机上链接并执行。

Sample errors: 错误样例:

    /home/ec2-user/httpd-2.4.2/srclib/apr/locks/unix/proc_mutex.c:367: undefined reference to `pthread_mutexattr_init'
    /home/ec2-user/httpd-2.4.2/srclib/apr/locks/unix/proc_mutex.c:374: undefined reference to `pthread_mutexattr_setpshared'
    /home/ec2-user/httpd-2.4.2/srclib/apr/locks/unix/proc_mutex.c:384: undefined reference to `pthread_mutexattr_setrobust_np'
    /home/ec2-user/httpd-2.4.2/srclib/apr/locks/unix/proc_mutex.c:393: undefined reference to `pthread_mutexattr_setprotocol'
    /home/ec2-user/httpd-2.4.2/srclib/apr/locks/unix/proc_mutex.c:414: undefined reference to `pthread_mutexattr_destroy'
    /home/ec2-user/httpd-2.4.2/srclib/apr/locks/unix/proc_mutex.c:408: undefined reference to `pthread_mutexattr_destroy'

Thanks 谢谢

These errors means you didn't add -pthread to your compile command line, so you're not getting the pthread library linked in. 这些错误意味着您没有在编译命令行中添加-pthread ,因此也没有链接到pthread库。

(Note: it's -pthread , not -lpthread - it's not just a linker option.) (注意:它是-pthread ,而不是-lpthread它不仅仅是一个链接器选项。)

So anyone else searching for this may get the answer. 因此,其他任何人都可以找到答案。

  1. Download whatever Apache version (higher than 2.2) source from the apache httpd site to the home directory 从apache httpd站点下载任何Apache版本(高于2.2)的源代码到主目录中
  2. Unpack 开箱
  3. Configure with everything or simply just specify no options (You can reconfigure later for the actual build) 使用所有内容进行配置或仅不指定任何选项(您可以稍后为实际构建重新配置)
  4. perform a make on the source ( don't do a make install yet! just need the object files) 在源代码上执行make(尚未进行make install!只需要目标文件)
  5. create a directory that will hold your libraries and source for your own module. 创建一个目录,该目录将保存您自己的模块的库和源。 Say "foo" at the top level of your home dir (or any you desire) 在您的主目录(或您想要的任何目录)的顶层说“ foo”
  6. assuming home dir: execute (find ~/httpdX.X -name '*.o' -exec cp {} ~/foo \\;) The command is in parenthesis. 假设主目录:execute(查找〜/ httpdX.X -name'* .o'-exec cp {}〜/ foo \\;)该命令用括号括起来。 This will copy all compiled objects to foo. 这会将所有编译的对象复制到foo。
  7. execute (find ~/foo -name '*.o' -exec ar r libhttpd.a {} \\;) which will create an archive of the code for you. 执行(查找〜/ foo -name'* .o'-exec ar libhttpd.a {} \\;)),这将为您创建代码的存档。
  8. Then just include these switches in your compile definition for gcc or g++ (-Wl,-Bstatic -lhttpd -lpcre -lpcreposix -lapr-1 -laprutil-1 -Wl,-Bdynamic -pthread -ldl -lcrypt) 然后只需将这些开关包含在gcc或g ++的编译定义中(-Wl,-Bstatic -lhttpd -lpcre -lpcreposix -lapr-1 -laprutil-1 -Wl,-Bdynamic -pthread -ldl -lcrypt)
  9. Clean up your foo directory if you like by running rm on the *.o files 如果愿意,可以通过在* .o文件上运行rm来清理foo目录

You do not need to compile statically like I needed to, but I wanted to be able to move my module to any Linux host without worry about the necessary components. 您不需要像我需要的那样进行静态编译,但是我希望能够将模块移动到任何Linux主机,而不必担心必要的组件。 Apache needs pcre(regex), apr(all libraries), threads (proc/thread mutex), dl(dynamic loading), and crypt(apr password) to work. Apache需要pcre(regex),apr(所有库),线程(proc /线程互斥体),dl(动态加载)和crypt(apr密码)才能工作。 Since thread, dl, and crypt will most like already be on the machine, I chose to not compile them statically. 由于线程,dl和crypt最像已经在计算机上,因此我选择不静态编译它们。

Happy hunting. 狩猎愉快。 I hope my never ending story over 3 days helps someone else! 我希望我三天无休止的故事能对其他人有所帮助!

For those building Apache modules in C++ and want dynamic linking, here's the g++ command line I used to successfully build a module; 对于那些用C ++构建Apache模块并想要动态链接的人,这是我用来成功构建模块的g ++命令行; briefly tested on Apache 2.2.22/CentOS 6.2. 在Apache 2.2.22 / CentOS 6.2上进行了简要测试。

g++ [my files].cpp  -I/httpd/include/ -I/httpd/srclib/apr/include/ 
    -I/httpd/srclib/apr-util/include/ -I/usr/include/ -I/usr/include/apr-1/ 
    -I/httpd/os/unix/ -shared -fPIC -o mod_mymodule.so

I'm an Apache/linux programming noob and was unable to find this info anywhere else; 我是一个Apache / linux编程新手,无法在其他任何地方找到此信息。 thanks to the OP's solution I was able to finish the job after a few days of frustration. 得益于OP的解决方案,我经过几天的挫折后才得以完成工作。

Here's also a link which helped explain how to work around the 'unresolved reference' linker issue when it couldn't find the apache functions contained within the httpd server core code (libhttpd.lib on Windows) -- which doesn't exist in *nix, unless you make it manually like the OP did. 这也是一个链接,该链接有助于解释当找不到httpd服务器核心代码(Windows上的libhttpd.lib)中包含的apache函数时,如何解决“未解决的引用”链接器问题-在*中不存在。 nix,除非您像OP那样手动进行设置。 Basically, the answer was to use the -shared flag so these references are automagically resolved at run time. 基本上,答案是使用-shared标志,以便在运行时自动解析这些引用。 http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html

Don't forget the 'extern C' in your module code, and build in DSO support when building HTTPD. 不要忘记模块代码中的“ extern C”,并且在构建HTTPD时会内置DSO支持。

Hope this helps someone else! 希望这对别人有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM