what is the use of gcc -fvisibility=hidden

I have a three files

1) cat my_lib.c
#include <stdio.h>
int fun()
{
return 2;
}

2) cat my_lib_shared.c

include "my_lib.h"
int foo(void)
{
return fun()+4;
}

3) cat driver.c

/* filename: driver.c */
#include "my_lib.h"
#include "my_lib_shared.h"
#include <stdio.h>
int main()
{
int x = fun() + foo();
printf("\n Final value is %d",x);
fflush(stdout);
return x;
}



I compile my_lib.c and make it static library with this file my_lib.c and shared library with my_shared.c

gcc -fvisibility=default -c -fPIC my_lib.c -o lib_mylib.o
ar rcs lib_mylib.a lib_mylib.o


gcc -c -Wall -Werror -fpic my_lib_shared.c
gcc -shared -o libmyshared.so my_lib_shared.o

When I link both libraries finally

gcc -L/home/rlalwani/SAMPLE_CODE/static -Wall -o test driver.c -lMyLib2.so -l_mylib
rlalwani@inn-rlalwani-vm[857] gcc -L. -Wall -o test driver.c -lMyLib2 -l_mylib
/usr/bin/ld: test: hidden symbol `fun' in ./lib_mylib.a(lib_mylib.o) is referenced by DSO
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status

I get following error

Please let me know your inputs and what is purpose of c -fvisibility=hidden flag
I have multiples files in file.c file2.c file3.c while making the static libraries out of these files
is it possible to keep symbols not hidden only in single file and symbols in rest of the files are hidden
Last edited on
Topic archived. No new replies allowed.