how to make our own library for c and cpp ? | Sololearn: Learn to code for FREE!

+1

how to make our own library for c and cpp ?

( can i done this with my mobile ? )

4/9/2021 6:48:40 AM

#...._._...._<........>

7 Answers

New Answer

+2

I dont know (sorry cuz i do my stuffs on emulated softwares :b), but termux does allow this, and i tested it. You can DM me for further assistance regarding termux, or visit its wiki page.

+2

BeegCat , i know this termux app but I haven't used that app yet. and i see my friend use this app. can you tell me link proper link of termux app ? because on the Play Store so many apps are there that's name is termux.

+2

Termux (Terminal emulator with packages) - https://f-droid.org/packages/com.termux This is the fdroid link, cuz playstore no longer will support future updates for termux due to changes in android policy

+2

There are two types of libraries. Static libraries and dynamic libraries. On Windows the convention is that static libraries have a .lib extension and dynamic libraries have a .dll extension. Under linux they are .a and .so respectively. Taking the case of static libraries, since they are the easiest, you compile your code into object code only then link it into a static library. Do not forget to provide header files for the library. This requires knowledge of your compiler and linker command line switches. Dynamic libraries are a little more complex and I won't go into them here.

+1

BeegCat , very nice answer, but still i have some doubts. ( you know any mobile app\compiler that support header file creation ? )

+1

The only tool you need to create a header file is a text editor. A header file such as stdio.h or iostream is nothing more that a plain text file. Never place function definitions, or any executable code, in a header file. You should only place the function declaration in a header file; the definition should be placed in a source file. // mystuff.h #ifndef MYSTUFF_H #define MYSTUFF_H int myfunction(int a, int b); #endif --- 8X -------------------------------- // mystuff.c int myfunction(int a, int b) { return a-b; } --- 8X -------------------------------- Compile the library into an object file... localhost> gcc -c -o mystuff.o mystuff.c Create the library using the linux ar tool... localhost> ar -crs libmystuff.a mystuff.o ar [options] archive-name object-file(s) -c = create archive -r = replace existing objects with new objects if they already exist -s = write an object-file index into the archive See: https://linux.die.net/man/1/ar NOTE: Your version of gcc and ar may have a prefix, such as arm-android-gcc, arm-android-ar or similar, if working under termux or with a cross compiler.

0

Header files basically contain function body and macro definitions. Basically make a file and store macro definitions like #define/ #if/ #pragma etc. If its a simple function, just add the definition of ur function and save this file as `somename.h`. You need not add the main function/ any other header file Coming to ur main code: Create a new file and save it as someothername.c and *include ur header file somename.h*. But dont add angular braces(<>), instead use double quotes (" ")and type in ur code and call the function. Save the code. Now comes the main part: for this to work, the header file you had made and the someothername.c file should be in the same folder. This should work. At least on pc. For phone, you need a compiler app which can support header file creation and similar stuffs.