How do I source/link external functions in C or C++?

前端 未结 3 440
天涯浪人
天涯浪人 2021-02-04 21:57

EDIT: I suppose I should clarify, in case it matters. I am on a AIX Unix box, so I am using VAC compilers - no gnu compilers. End edit

<
3条回答
  •  滥情空心
    2021-02-04 22:09

    You've got a third option. In general, your C++ compiler should be able to link C routines. The necessary options may vary from compiler to compiler, so R your fine M, but basically, you should be able to compile with g++ as here:

    $ g++ -o myapp myapp.cpp myfunc.c giveint.c
    

    ... or compile separately

    $ gcc -c myfunc.c
    $ gcc -c giveint.c
    $ g++ -c myapp.cpp
    $ g++ -o myapp myapp.o myfunc.o
    

    You also need to include your declaration of the functions; you do that in C++ as

    extern "C" {
        int myfunc(int,int);
        int giveInterger(void);
    }
    

提交回复
热议问题