build.sh
module1.c
module1.h
module2.c
module2.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# BUILD library which contains all modules of the package | |
echo"Build package.o from module1.c and module2.c" | |
gcc -c -o module1.o module1.c | |
gcc -c -o module2.o module2.c | |
ld -o package.o -r module1.o module2.o | |
symbols=(`nm package.o`) | |
for((i=0;i<${#symbols[*]};i=i+1))do | |
if [ "${symbols[$i]}"!="T" ];thencontinue;fi | |
function_name=${symbols[$((i+1))]} | |
if [ "${function_name#private}"="$function_name" ];thencontinue;fi | |
echo"Change function '$function_name' to package scope (private)" | |
objcopy -L "$function_name" package.o | |
done | |
echo"Generate main.c and link it with package.o" | |
echo"main() calls public_function from module1.c in package.o" | |
echo"#include <stdio.h>"> main.c | |
echo"#include \"module1.h\"">> main.c | |
echo>> main.c | |
echo"int main(int argc, const char* argv[])">> main.c | |
echo"{">> main.c | |
echo" public_function();">> main.c | |
echo" return 0;">> main.c | |
echo"}">> main.c | |
gcc -o main main.c package.o | |
echo"::Start main::" | |
./main | |
echo"::End main::" | |
echo | |
echo"Generate main.c and link it with package.o" | |
echo"main() now tries to call private_function from module2.c in package.o" | |
echo"This will generate a compiler error !" | |
gcc -o main "-Dpublic_function=private_function" main.c package.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include"module1.h"// header of this module | |
#include"module2.h"// contains private_function | |
voidpublic_function() | |
{ | |
printf("enter public_function()\n"); | |
private_function(); | |
printf("exit public_function()\n"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndefPACKAGE_MODULE1_HEADER | |
#definePACKAGE_MODULE1_HEADER | |
voidpublic_function(void); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include"module2.h"// header of this module | |
voidprivate_function() | |
{ | |
printf("enter private_function()\n"); | |
printf("exit private_function()\n"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndefPACKAGE_MODULE2_HEADER | |
#definePACKAGE_MODULE2_HEADER | |
voidprivate_function(void); | |
#endif |