Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a library in linux from c++ code with external references

Am having a problem with external references of libraries. I need to access the ogg libraries from xiph.org. I have simplified it down to the following. This works: in main.cpp

   #include "OpusInfoTest.h"

   int main (int arggc, char *argv[]) {
       OpusInfoTest *o = new OpusInfoTest("sampletest.opus");
       delete (o);
       return 0;
   }

in OpusInfoTest.cpp

#include "OpusInfoTest.h"

    OpusInfoTest::OpusInfoTest (char *qualifiedOpusFile) {
        printf ("File %s\n", qualifiedOpusFile);
        ogg_sync_state ogsync;
        ogg_sync_init(&ogsync);
    
    }
    
    OpusInfoTest::~OpusInfoTest () {
        printf ("Gone\n");
    }

and OpusInfoTest.h

#pragma once

#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ogg.h>



class OpusInfoTest {
    public:
        OpusInfoTest(char *qualifiedOpusFile);
        ~OpusInfoTest(void);

};

Compiled with

g++ main.cpp OpusInfoTest.cpp -I ../ogg/buildl/include/ogg -I ../ogg/buildl/include -I ../opustools/src -I ../ogg/include/ogg -I ../ogg/include/ -Wno-write-strings -L ../ogg/buildl -l:libogg.a

Works fine. I then created a library OIT.a with

g++ -c -o OIT.a OpusInfoTest.cpp -I ../ogg/buildl/include/ogg -I ../ogg/buildl/include -I ../opustools/src -I ../ogg/include/ogg -I ../ogg/include/ -Wno-write-strings -L ../ogg/buildl -l:libogg.a

And then tried to use it with main.cpp

 g++ main.cpp -I ../ogg/buildl/include/ogg -I ../ogg/buildl/include -I ../opustools/src -I ../ogg/include/ogg -I ../ogg/include/ -Wno-write-strings -L ../ogg/buildl -l:libogg.a -L . -l:OIT.a

returns link error

buildl -l:libogg.a -L . -l:OIT.a
/usr/bin/ld: ./OIT.a: in function `OpusInfoTest::OpusInfoTest(char*)':
OpusInfoTest.cpp:(.text+0x33): undefined reference to `ogg_sync_init'
collect2: error: ld returned 1 exit status

Probably missing something simple. New to building libraries on linux

like image 256
Peter Bonham Avatar asked Nov 17 '25 07:11

Peter Bonham


1 Answers

Compile to object files like:

g++ -c OpusInfoTest.cpp -I ../ogg/buildl/include/ogg [other include paths]

You should get a file called OpusInfoTest.o (not a library yet).

Then you can use the ar command to create libOIT.a , which is a static library.

ar rcs libOIT.a OpusInfoTest.o

Lastly, link everything in the correct order:

g++ main.cpp [include paths] -L. -lOIT -L../ogg/buildl -logg

You need to make sure your libOIT.a library shows up first in the linking above, since it depends on the other libogg.a library.

like image 156
Pratik Mathur Avatar answered Nov 19 '25 22:11

Pratik Mathur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!