简体   繁体   English

包括C语言中的头文件和编译

[英]including header files in C and compile

I am working with an open source project called snort , which is written in C, under Linux. 我正在使用一个名为snort的开源项目,该项目是用Linux编写的。 I opened project in netbeans correctly and now I am going to do some changes in this source code. 我正确地在netbeans中打开项目,现在我将对此源代码进行一些更改。 The src folder of program contains several folders and also each folder has some folders. 程序的src文件夹包含几个文件夹,每个文件夹也有一些文件夹。 I heard that netbeans is able to generate make files. 我听说netbeans能够生成make文件。 I am doing some changes in src files in folder XFolder and want to use a library function in another folder in my project (YFolder). 我正在对文件夹XFolder中的src文件进行一些更改,并希望在我的项目(YFolder)中的另一个文件夹中使用库函数。 I included .h file and used the function correctly. 我包含.h文件并正确使用了该功能。

#include"../YFolder/lib.h"

Now when I can compile the program, It is Ok, but when I use dynamic libraries ".so (shared object files)" that created in make process; 现在,当我可以编译程序时,它是好的,但是当我使用在make进程中创建的动态库“.so(共享对象文件)”时; and run program, I see an error that means the function I used from other folder not defined and see this error; 并运行程序,我看到一个错误,这意味着我从其他未定义的文件夹中使用的函数,并看到此错误; (sfxhash_new is the name of external function that I called). (sfxhash_new是我调用的外部函数的名称)。

libsf_sip_preproc.so: undefined symbol: sfxhash_new libsf_sip_preproc.so:未定义的符号:sfxhash_new

I also edited Makefile.am and added the sources of that package ( ../YFolder/lib.c and lib.h ); 我还编辑了Makefile.am并添加了该包的源代码( ../YFolder/lib.c and lib.h ); But not effective. 但没有效果。 Can anyone help me please? 有人可以帮我吗?

EDIT: 编辑:

I am in folder src/dynamic-preprocessor/sip I want to use a function in file: src/sfutil/sfxHash.c the function name is sfxhash_new(... ... ...) I included sfxHash.h correctly. 我在文件夹src / dynamic-preprocessor / sip我想在文件中使用一个函数:src / sfutil / sfxHash.c函数名是sfxhash_new(...... ......)我正确地包含了sfxHash.h。 I did some changes In my Makefile.am but the main makefile is this. 我在Makefile.am中做了一些更改,但主要的makefile就是这个。

My Makefile.am file: 我的Makefile.am文件:

## $Id
AUTOMAKE_OPTIONS=foreign no-dependencies

INCLUDES = -I../include -I${srcdir}/../libs -I$(srcdir)/includes

libdir = ${exec_prefix}/lib/snort_dynamicpreprocessor

lib_LTLIBRARIES = libsf_sip_preproc.la

libsf_sip_preproc_la_LDFLAGS = -shared -export-dynamic -module @XCCFLAGS@
if SO_WITH_STATIC_LIB
libsf_sip_preproc_la_LIBADD = ../libsf_dynamic_preproc.la
else
nodist_libsf_sip_preproc_la_SOURCES = \
../include/sf_dynamic_preproc_lib.c \
../include/sf_ip.c \

endif

libsf_sip_preproc_la_SOURCES = \
spp_sip.c \
spp_sip.h \
sip_config.c \
sip_config.h \
sip_parser.c \
sip_parser.h \
sip_dialog.c \
sip_dialog.h \
sip_roptions.c \
sip_roptions.h \
sip_utils.c \
sip_utils.h \
sip_debug.h \
../include/sfxhash.c \   -----------------> I have copied src files in this dictionary
../include/sfxhash.h     ------------------>

EXTRA_DIST = \
sf_sip.dsp

all-local: $(LTLIBRARIES)
    $(MAKE) DESTDIR=`pwd`/../build install-libLTLIBRARIES

After making changes in Makefile.am file, the changes are not reflected immediately (ie if you run configure & make you will not see the changes). Makefile.am文件中进行更改后,更改不会立即反映出来(例如,如果运行configuremake您将看不到更改)。 You should generate/update the corresponding Makefile.in file first. 您应该首先生成/更新相应的Makefile.in文件。 For that you need to run automake command in the topmost directory of the source tree (where configure.in or configure.ac resides). 为此,您需要在源树的最顶层目录中运行automake命令( configure.inconfigure.ac所在的目录)。 To make sure that your Makefile.am changes to include new sources will successfully be reflected in the build, check that libsf_sip_preproc_la_SOURCES is same set of files in Makefile.am as well as Makefile.in . 要确保Makefile.am更改为包含新源将成功反映在构建中,请检查libsf_sip_preproc_la_SOURCES是否与Makefile.am以及Makefile.in的文件相同。 Now, run configure and make commands. 现在,运行configuremake命令。
Note that adding a file from one place to another in the source tree may bring in its own set of dependencies ie sfxhash source files may include files & link to libraries which are not present as part of Makefile.am in question, in which case you may have to update INCLUDES to include the directory needed by the source and/or add new libraries in libsf_sip_preproc_la_LIBADD . 请注意,在源树中将文件从一个地方添加到另一个地方可能会带来自己的一组依赖项,即sfxhash源文件可能包含文件和指向库中的链接,这些库不是Makefile.am一部分,在这种情况下,可能必须更新INCLUDES以包含源所需的目录和/或在libsf_sip_preproc_la_LIBADD添加新库。 Avoid mixing .la & .a files in libsf_sip_preproc_la_LIBADD . 避免在libsf_sip_preproc_la_LIBADD混合使用.la.a文件。
Hope this helps! 希望这可以帮助!

As you have written: 如你所写:

libsf_sip_preproc_la_LDFLAGS = -shared -export-dynamic -module @XCCFLAGS@
if SO_WITH_STATIC_LIB
libsf_sip_preproc_la_LIBADD = ../libsf_dynamic_preproc.la
else
nodist_libsf_sip_preproc_la_SOURCES = \
../include/sf_dynamic_preproc_lib.c \
../include/sf_ip.c \

endif

if SO_WITH_STATIC_LIB is true, I think this line: 如果SO_WITH_STATIC_LIB为真,我想这一行:

libsf_sip_preproc_la_LIBADD = ../libsf_dynamic_preproc.la

should be 应该

libsf_sip_preproc_la_LIBADD = ../libsf_dynamic_preproc.a

that is my idea, you can try it. 这是我的想法,你可以尝试一下。

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

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