[GH-ISSUE #54] move code from *.h to *.c #170

Closed
opened 2026-03-13 16:43:14 +03:00 by kerem · 10 comments
Owner

Originally created by @RekGRpth on GitHub (Jul 22, 2021).
Original GitHub issue: https://github.com/ADD-SP/ngx_waf/issues/54

please, move all code from *.h to *.c

Originally created by @RekGRpth on GitHub (Jul 22, 2021). Original GitHub issue: https://github.com/ADD-SP/ngx_waf/issues/54 please, move all code from *.h to *.c
kerem 2026-03-13 16:43:14 +03:00
Author
Owner

@ADD-SP commented on GitHub (Jul 22, 2021):

Do you mean rename all *.h to *c? I don't know why you would want to do that.

If you mean splitting the headers into .h and .c, then from a software engineering point of view we should do that, but it will cause more warnings and errors when the module is compiled.

To avoid conflicts with other modules or with nginx's own symbols, we develop modules by declaring functions as static, so that symbols inside a source file are not exposed to the outside unless the #include directive is used, and so that symbol conflicts can occur during compilation and linking.

If the declaration is written in *.h and the implementation is written in *.c. then an error will occur at compile time because of the undefined symbols. So I have to put both in one file.

But if you have a way to solve this problem, please advise.

<!-- gh-comment-id:884804689 --> @ADD-SP commented on GitHub (Jul 22, 2021): Do you mean rename all `*.h` to `*c`? I don't know why you would want to do that. If you mean splitting the headers into `.h` and `.c`, then from a software engineering point of view we should do that, but it will cause more warnings and errors when the module is compiled. To avoid conflicts with other modules or with nginx's own symbols, we develop modules by declaring functions as `static`, so that symbols inside a source file are not exposed to the outside unless the `#include` directive is used, and so that symbol conflicts can occur during compilation and linking. If the declaration is written in `*.h` and the implementation is written in `*.c`. then an error will occur at compile time because of the undefined symbols. So I have to put both in one file. But if you have a way to solve this problem, please advise.
Author
Owner

@RekGRpth commented on GitHub (Jul 22, 2021):

Yes, declarations should be in *.h and implementation in *.c

To avoid errors You should does not make static functions that used in many *.c, i.e. if some function is used only in current *.c file then it shuld be static, else - not

<!-- gh-comment-id:884808172 --> @RekGRpth commented on GitHub (Jul 22, 2021): Yes, declarations should be in *.h and implementation in *.c To avoid errors You should does not make static functions that used in many *.c, i.e. if some function is used only in current *.c file then it shuld be static, else - not
Author
Owner

@RekGRpth commented on GitHub (Jul 22, 2021):

Consider static functions as private method and non-static functions as public ones.

And add prefix to all Your public functions with Your unique prefix like ngx_http_waf_ and their names does not conflict with others.

<!-- gh-comment-id:884809406 --> @RekGRpth commented on GitHub (Jul 22, 2021): Consider static functions as private method and non-static functions as public ones. And add prefix to all Your public functions with Your unique prefix like ngx_http_waf_ and their names does not conflict with others.
Author
Owner

@ADD-SP commented on GitHub (Jul 22, 2021):

Consider static functions as private method and non-static functions as public ones.

And add prefix to all Your public functions with Your unique prefix like ngx_http_waf_ and their names does not conflict with others.

I actually considered this way before, but I felt that the code would become more difficult to read (although it is already difficult to read), so I have put it off until now.

<!-- gh-comment-id:884819208 --> @ADD-SP commented on GitHub (Jul 22, 2021): > > > Consider static functions as private method and non-static functions as public ones. > > And add prefix to all Your public functions with Your unique prefix like ngx_http_waf_ and their names does not conflict with others. I actually considered this way before, but I felt that the code would become more difficult to read (although it is already difficult to read), so I have put it off until now.
Author
Owner

@RekGRpth commented on GitHub (Jul 22, 2021):

https://github.com/ADD-SP/ngx_waf is compiled as single *.c file
I found only one project that do so https://github.com/wandenberg/nginx-push-stream-module
but other projects put declarations in *.h and implementation in *.c like https://github.com/nbs-system/naxsi

<!-- gh-comment-id:884825945 --> @RekGRpth commented on GitHub (Jul 22, 2021): https://github.com/ADD-SP/ngx_waf is compiled as single *.c file I found only one project that do so https://github.com/wandenberg/nginx-push-stream-module but other projects put declarations in *.h and implementation in *.c like https://github.com/nbs-system/naxsi
Author
Owner

@ADD-SP commented on GitHub (Jul 22, 2021):

I should not have put this off for so long and I will address it in the near future.

<!-- gh-comment-id:884850158 --> @ADD-SP commented on GitHub (Jul 22, 2021): I should not have put this off for so long and I will address it in the near future.
Author
Owner

@ADD-SP commented on GitHub (Jul 23, 2021):

Done on branch dev.

<!-- gh-comment-id:885409089 --> @ADD-SP commented on GitHub (Jul 23, 2021): Done on branch dev.
Author
Owner

@RekGRpth commented on GitHub (Jul 23, 2021):

thanx, but it does not comile until fix

diff --git a/inc/ngx_http_waf_module_config.h b/inc/ngx_http_waf_module_config.h
index b5740a8..328554b 100644
--- a/inc/ngx_http_waf_module_config.h
+++ b/inc/ngx_http_waf_module_config.h
@@ -11,7 +11,6 @@
 
 #include <string.h>
 #include <utarray.h>
-#include <ngx_cycle.h>
 #include <ngx_http_waf_module_macro.h>
 #include <ngx_http_waf_module_type.h>
 #include <ngx_http_waf_module_util.h>
<!-- gh-comment-id:885418045 --> @RekGRpth commented on GitHub (Jul 23, 2021): thanx, but it does not comile until fix ```diff diff --git a/inc/ngx_http_waf_module_config.h b/inc/ngx_http_waf_module_config.h index b5740a8..328554b 100644 --- a/inc/ngx_http_waf_module_config.h +++ b/inc/ngx_http_waf_module_config.h @@ -11,7 +11,6 @@ #include <string.h> #include <utarray.h> -#include <ngx_cycle.h> #include <ngx_http_waf_module_macro.h> #include <ngx_http_waf_module_type.h> #include <ngx_http_waf_module_util.h> ```
Author
Owner

@ADD-SP commented on GitHub (Jul 23, 2021):

thanx, but it does not comile until fix

diff --git a/inc/ngx_http_waf_module_config.h b/inc/ngx_http_waf_module_config.h
index b5740a8..328554b 100644
--- a/inc/ngx_http_waf_module_config.h
+++ b/inc/ngx_http_waf_module_config.h
@@ -11,7 +11,6 @@
 
 #include <string.h>
 #include <utarray.h>
-#include <ngx_cycle.h>
 #include <ngx_http_waf_module_macro.h>
 #include <ngx_http_waf_module_type.h>
 #include <ngx_http_waf_module_util.h>

Done.

github.com/ADD-SP/ngx_waf@0763c32f86/inc/ngx_http_waf_module_config.h (L12-L22)

<!-- gh-comment-id:885419772 --> @ADD-SP commented on GitHub (Jul 23, 2021): > > > thanx, but it does not comile until fix > > ```diff > diff --git a/inc/ngx_http_waf_module_config.h b/inc/ngx_http_waf_module_config.h > index b5740a8..328554b 100644 > --- a/inc/ngx_http_waf_module_config.h > +++ b/inc/ngx_http_waf_module_config.h > @@ -11,7 +11,6 @@ > > #include <string.h> > #include <utarray.h> > -#include <ngx_cycle.h> > #include <ngx_http_waf_module_macro.h> > #include <ngx_http_waf_module_type.h> > #include <ngx_http_waf_module_util.h> > ``` Done. https://github.com/ADD-SP/ngx_waf/blob/0763c32f861f7e3166955ac5de482ef8fd5e8b7c/inc/ngx_http_waf_module_config.h#L12-L22
Author
Owner

@RekGRpth commented on GitHub (Jul 23, 2021):

thanx, now it compiles!

<!-- gh-comment-id:885420744 --> @RekGRpth commented on GitHub (Jul 23, 2021): thanx, now it compiles!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/ngx_waf#170
No description provided.