GCC: How to customising warning and errors generated from compilation

时光怂恿深爱的人放手 提交于 2021-02-11 12:51:38

问题


My Usecase: I have two c language files: ApplicationCode.c and UserCode.c . Application code is something generated by my app and UserCode is available to my application user where he can write his own code. ApplicationCode.c internally uses UserCode.c and calls out its methods/function.

One thing you can assume here that ApplicationCode.c will never have any errors but could have warnings generated by gcc compiler. And UserCode.c can have both errors and warnings. So, While compiling code I wanted to show user his code's errors as first priority. If needed to show warning also then show warning generated by his code only not from ApplicationCode.c (As i don't want to expose my ApplicationCode.c's code). One more constraint is here that I wanted to show return-type warning as error here and for this I am using -Werror=return-type argument.

So, I am thinking of following three approaches to do this:

  1. If I could able to disable all the warnings and enable only -Wreturn-type
    Raised a saparate question for this here

  2. If I could able to instruct gcc (or redirect) only error messages to stderr and rest on stdout. Raised a saparate question for this also here

  3. If I could able to disable all the warnings (which are enabled by default) from ApplicationCode.c file and enable all with -Werror=return-type for UserCode.c

I tried searching everywhere but did not get any solution for all the above 3. Let me know how could I achieve above problems or if there any other better way to do solve my use case ?

Update 1: Here is my both the code file look like

ApplicationCode.c

#include <stdio.h>
// some headers
// some application specific code

int testUserFunction(); // user function declaration

int main(int argc, char *a[]) {
   int result  = testUserFunction(); // calling user function

   // some logic to use and evaluate result
}

UserCode.c

#include<stdio.h>

int testUserFunction(int input1)
{
    // user will write his code below this

    // user code
    
}

Base command to compile code:

gcc -o code.out ApplicationCode.c UserCode.c 


回答1:


or if there any other better way to do solve my use case ?

If you don't want to "expose ApplicationCode.c", then why are you showing its compilation output in the first place? Solution: Compile it in somewhere secret and let the user link their UserCode.c with it.



来源:https://stackoverflow.com/questions/66060224/gcc-how-to-customising-warning-and-errors-generated-from-compilation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!