How to ignore undefined characters at runtime on Linux?

I know that by default undefined characters are ignored at compile time. However, I would also like to be ignored at runtime. I need to distribute .so, which will work with and without MPI. I will know in advance if this is an MPI job, and if it is not, I will not make any MPI_ * calls. If this is not starting MPI, I need the application to not take care that it cannot resolve MPI_ * characters.

Is it possible? I could have sworn I did it before, but I can't get it to work. Every time I run, I immediately get the following, even if the logic in my code will never allow reference to this symbol:

undefined symbol: hpmp_comm_world

Why do I use the Intel Fortran compiler to build the .so file.

EDIT

I found the linker flag: "-z lazy", which should allow function references when calling a function, which is what I want. This does not fix my problem, but hpmp_comm_world is a variable, not a function. Should it make a difference?

+3
source share
1 answer

You can define a character as a weak reference to define it. Then the value of the symbol will be zero if there is no definition.

For example, suppose you are given ref.c, which refers to a function and variable that may or may not be present; we will use it to build libref.so (which corresponds to your library in your question):

#include <stdio.h>

void global_func(void);
void global_func(void) __attribute__ ((weak));

extern int global_variable __attribute__((weak));

void ref_func() {
  printf("global_func = %p\n", global_func);
  if (&global_variable)
    global_variable++;
  if (global_func)
    global_func();
}

global_func global_variable . , , , , . ( , address , , &global_variable .)

def.c, global_func global_variable; libdef.so( MPI ):

#include <stdio.h>

int global_variable;

void global_func(void) {
  printf("Hi, from global_func!  global_variable = %d\n", global_variable);
}

, , , main.c, ref_func libref.so:

#include <stdio.h>

extern void ref_func(void);

int main(int argc, char **argv) {
  printf("%s: ", argv[0]);
  ref_func();
  return 0;
}

Makefile, libref.so libdef.so, , libref.so, libdef.so:

all: ref-absent ref-present
ref-absent: main.o libref.so
    $(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@
ref-present: main.o libref.so libdef.so
    $(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@

lib%.so: %.o
    $(CC) $(CFLAGS) $(LDFLAGS) -shared $^ -o $@

ref.o def.o: CFLAGS += -fpic

clean:
    rm -f *.o *.so ref-absent ref-present

:

$ make
cc    -c -o main.o main.c
cc -fpic   -c -o ref.o ref.c
cc   -shared ref.o -o libref.so
cc   main.o libref.so -o ref-absent
cc -fpic   -c -o def.o def.c
cc   -shared def.o -o libdef.so
cc   main.o libref.so libdef.so -o ref-present
$ 

, ref-absent, ref-present , global_name ref-missing.

, ref-missing , ref-present . ( LD_LIBRARY_PATH, .)

$ LD_LIBRARY_PATH=. ./ref-absent
./ref-absent: global_func = (nil)
$ LD_LIBRARY_PATH=. ./ref-present
./ref-present: global_func = 0x15d4ac
Hi, from global_func!  global_variable = 1
$ 

((weak)), MPI, . , Ref.c, , , , . , , - ( MPI):

#include <mpi.h>

mpi_fake_type_t mpi_function_foo(mpi_arg_type_t) __attribute__((weak));
mpi_fake_type_t mpi_function_bar(mpi_other_arg_type_t) __attribute__((weak));

MPI (()) ; , . , , .

+6

Source: https://habr.com/ru/post/1757876/


All Articles