Sign in to follow this  
Viking

Plugin with multiple source files

Recommended Posts

Posted (edited)

Hello,

 

is it possible to build a plugin from multiple source files.
I tried to create a library but still get a unknown symbol error for a function call, no matter if it's a static or a shared library.

 

Maybe i have a fault in the compile process.

Static try:

#create static library
gcc -m32 -c source_a.c -o bin/static/source_a.o
gcc -m32 -c source_b.c -o bin/static/source_b.o
gcc -m32 -c source_c.c -o bin/static/source_c.o
ar rcs bin/static/libtest.a bin/static/source_a.o bin/static/source_b.o bin/static/source_c.o

#link the static library and build the final plugin
gcc -m32 -shared -L bin/static -l test -fPIC main.c -o testplugin.so

 

Shared try:

#create shared library
gcc -m32 -c -fPIC source_a.c -o bin/shared/source_a.o
gcc -m32 -c -fPIC source_b.c -o bin/shared/source_b.o
gcc -m32 -c -fPIC source_c.c -o bin/shared/source_c.o
gcc -m32 -shared bin/shared/source_a.o bin/shared/source_b.o bin/shared/source_c.o -o bin/shared/libtest.so

#link the shared library and build the final plugin
#gcc -m32 -shared -L bin/shared -l test -fPIC main.c -o testplugin.so

 

both compiles fine but fail on server launch with this error:

]./cod4x18_dedrun: symbol lookup error: /home/viking/cod4/server/plugin.testplugin.tmp: undefined symbol: foo_bar_test

 

 

edit: i have the plugin in cod4x folder only, in case this info is needed.

Edited by Viking

Share this post


Link to post
Share on other sites

not sure where the issue is, those lines look ok. did you export the function? how does the code look?

Share this post


Link to post
Share on other sites

This is what i have right now, just some test files yet... nothing great :D

 

main.c
 

#include "../pinc.h"
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

/*########################
functions of the plugin
########################*/



/*########################
required plugin init
########################*/
/*============
Function used to load the new functions and methods of the plugin
============*/
PCL int OnInit()
{
    Plugin_Printf("^5Multiple Source Test Plugin");

    foo_bar_test();

    int valSrc_a = test_func_source_a();
    int valSrc_b = test_func_source_b();
    int valSrc_c = test_func_source_c();

    Plugin_Printf("^5Multiple Source Test Plugin - done");

    return 0;
}

/*============
Function used to obtain information about the plugin
Memory pointed by info is allocated by the server binary, just fill in the fields
============*/
PCL void OnInfoRequest(pluginInfo_t *info)
{
    // =====  MANDATORY FIELDS  =====
    info->handlerVersion.major = PLUGIN_HANDLER_VERSION_MAJOR;
    info->handlerVersion.minor = PLUGIN_HANDLER_VERSION_MINOR;    // Requested handler version

    // =====  OPTIONAL  FIELDS  =====
    info->pluginVersion.major = 1;    // Plugin version
    info->pluginVersion.minor = 0;    // Plugin sub version
    strncpy(info->fullName,"Test",sizeof(info->fullName)); //Full plugin name
    strncpy(info->shortDescription,"Test Desc Short",sizeof(info->shortDescription)); // Short plugin description
    strncpy(info->longDescription,"Test Dec Long",sizeof(info->longDescription));  // Long plugin description
}

 

source_a.c

#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int test_func_source_a()
{
    Plugin_Printf("^5Called func in source_a");
    return 1;
}

void foo_bar_test()
{
    Plugin_Printf("^5foo_bar_test");
}

 

source_b.c

#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int test_func_source_b()
{
    Plugin_Printf("^5Called func in source_b");
    return 2;
}

 

source_c.c

#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int test_func_source_c()
{
    Plugin_Printf("^5Called func in source_c");
    return 3;
}

 

Share this post


Link to post
Share on other sites

main.c needs to know about foo_bar_test() function. declare it in main.c as extern. that's usually done in a header file.

extern void foo_bar_test()

Share this post


Link to post
Share on other sites

a minimal example to build a shared library:

// main.c

extern void testfun();

void main()
{
    testfun();
}

// foobar.c

#include <stdio.h>

void testfun() 
{
    printf("testfun");
}

 

// build.sh

rm main
gcc -c -fPIC foobar.c -o foobar.o
gcc -shared -fPIC foobar.o -o libfoobar.so
gcc -c main.c -o main.o
gcc main.o ./libfoobar.so -o main
./main

 

Share this post


Link to post
Share on other sites
Posted (edited)
21 hours ago, dpj said:

main.c needs to know about foo_bar_test() function. declare it in main.c as extern. that's usually done in a header file.


extern void foo_bar_test()

I added this to my main.c and also tried it within an inlcuded main.h

Sadly i still get the undefined symbol: foo_bar_test error message when running the plugin on the server.

GCC compiles fine.

 

21 hours ago, dpj said:

a minimal example to build a shared library:

// main.c


extern void testfun();

void main()
{
    testfun();
}

// foobar.c


#include <stdio.h>

void testfun() 
{
    printf("testfun");
}

 

// build.sh


rm main
gcc -c -fPIC foobar.c -o foobar.o
gcc -shared -fPIC foobar.o -o libfoobar.so
gcc -c main.c -o main.o
gcc main.o ./libfoobar.so -o main
./main

 

 

this example compiles a litte program, which works, but not a plugin for cod4x.

i tried to add plugin support and now ended with

 

gcc -m32 -c -fPIC source_a.c -o source_a.o
gcc -m32 -c -fPIC source_b.c -o source_b.o
gcc -m32 -c -fPIC source_c.c -o source_c.o
gcc -m32 -shared -fPIC source_a.o source_b.o source_c.o -o bin/shared/libtest.so
gcc -m32 -shared -fPIC main.c -L bin/shared -l test -o testplugin.so

which results in

Error: Sys_LoadLibrary error: libtest.so: cannot open shared object file: No such file or directory
Error: Failed to load the plugin /home/viking/cod4/server/plugin.testplugin.tmp!

where do i have to place the library to?

 

 

edit:

nvm, i have build a static library, now the exmaple plugin works.
thanks :)

Edited by Viking

Share this post


Link to post
Share on other sites
4 hours ago, Viking said:

where do i have to place the library to?

https://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html3.5. Installing and Using a Shared Library

unlike windows, linux does not check the working directory for libraries typically, unless you tell it to.

 

that's also where my example from above comes in again. it shows the basic concepts. now with a tiny change:

rm main
gcc -c -fPIC foobar.c -o foobar.o
gcc -shared -fPIC foobar.o -o libfoobar.so
gcc -c main.c -o main.o
# gcc main.o ./libfoobar.so -o main
gcc main.o -lfoobar -L. -o main
LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH ./main

 

4 hours ago, Viking said:

I added this to my main.c and also tried it within an inlcuded main.h 

not defining the symbol at all leads to a warning for implicit declaration. the correct place to define it would probably be "foobar.h" as the function belongs to the foobar library, not the main program.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this