How do I make a .a library?

Pages: 12
Hello. I want to know how to make a library (.a file)
that I can use in projects on all operating systems/devices
(3ds, etc.) How would I go about doing this in VS2019?

Any help is appreciated.

Thanks in advance.
closed account (z05DSL3A)
https://docs.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-static-library-cpp
That is Windows only, I thought... Is it not?
closed account (z05DSL3A)
Sorry, thought you were after a general starting point...you would probably want to setup a cmake project in VS to target different platforms.
Ok, not very sure what cmake is, so I'll look into it a bit. Thanks!
CMake is a cross-platform tool for making build-tools. It allows you to write one codeset to specifiy the build tools you want, and then it will build the tools for you depending on what system you're using. So, on Linux, it will create your makefiles for you, and on Windows it will create the Visual Studio solutions and projects, etc.

It's complicated and difficult to learn, and not necessary for this.

If you're using gcc, this looks like a good start for you:

https://renenyffenegger.ch/notes/development/languages/C-C-plus-plus/GCC/create-libraries/index
closed account (z05DSL3A)
MikeyBoy wrote:
CMake is a cross-platform tool for making build-tools. It allows you to write one codeset to specifiy the build tools you want, and then it will build the tools for you depending on what system you're using.
I'm not sure I agree with you there, it is a cross-platform build system (?)
"CMake is an open-source, cross-platform family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files, and generate native makefiles and workspaces that can be used in the compiler environment of your choice." -- https://cmake.org/

and wrote:
It's complicated and difficult to learn, and not necessary for this.

You find it complicated and difficult to learn...okay. I think the basics are pretty easy to get you stated. As to if it's necessary, the OP seems to want a single codebase built out to several different platforms, what do you suggest?
Ah, I'd missed that the OP wanted this for multiple platforms. So it might be suited to the OP's requirements.

I stand by what I said about it being rather complicated. I mean, it's doing a very complicated and difficult job, so I wouldn't expect it to be simple to learn and use. It's an extremely powerful and useful tool, and it's one I use myself - I would never go back to trying to maintain multiple build sustems again. It's no more complicated than it needs to be, and there's no need to jump to its defence.

But it's not something I would expect someone who's still fairly close to the start of the programming learning curve to learn easily.

To the OP: what build tools are you already using on the multiple platforms you want to develop on?
closed account (z05DSL3A)
Ah, I'd missed that the OP wanted this for multiple platforms.
Can't blame you for that, I missed it's importance with my first reply.

I wasn't jumping to cmakes defence or attacking you or anything like that, I was just a bit surprised by what you wrote. I guess I have the view that it is best to start using it early while your demands on it are on the relatively easier end of the learning curve...and the OP mentioned Visual Studio and the tooling for cmake it it look good...

Anyway, have a good day.

If you have mingw on your computer (gcc, g++ e.t.c.) , then it is easy to make a static (.a) library.

g++ -c myfile.cpp
(This gets you myfile.o)
then
ar rcs -o libmyfile.a myfile.o

The output file is libmyfile.a

(You don't need any int main() in your myfile.cpp), only your functions.

I can point you to a mingw .7z compressed file if you want, no installation, just unzip somewhere and put the bin folder on path.
Then you will have gcc, g++, gnu fortran, and many other useful functions at hand.
Whether or not your newly created libmyfile.a will do for Visual Studio C++ I don't know. I know than many libraries (example: gmp) are from gcc (gnu project).







Ok, so, I figured out how to build a library, but when I try to compile,
("g++ -o main main.cpp -L. libGUI.a")

I get the following error:
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: libGUI.a: error adding symbols: archive has no index; run ranlib to add one


Any idea why?

EDIT: The compile commands for the library are as follows:
g++ -c -o GUI.o GUI.cpp
ar rcv libGUI.a GUI.o
Last edited on
Your ar command should be

ar rcs libGUI.a GUI.o

The s causes it to write an index.
Oh, ok. I'll try that out.

EDIT: Got the same error after trying that... :/
Last edited on
oggin said,
I can point you to a mingw .7z compressed file if you want


I have something called TDM installed, is that it?
Got the same error after trying that... :/

Then you must have done something wrong.
Well, not to be rude, but I think that has already been established...

OK, I tested this
step 1
primes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

#include<math.h>



int isprime(unsigned long long n)
{		
 if (n == 2 | n == 3)
{
  return (-1);
}

  if ( n % 2 == 0)
  {
  return(0);
}
  
   if ( (n % 3)== 0)
   {
  return(0);
}

 unsigned long long limit=(unsigned long long) sqrt(n)+1;
 unsigned long long i;
 
  for (  i = 6  ; i < limit ; i = i + 6 )
  {
  	
        if (n % (i-1) == 0)
		{
        return(0);
        }
         if (n % (i+1) == 0)
         {
        return(0);
          }
    
    }
  return(-1);	
}


  


then
g++ -c primes.cpp

I now have
primes.o

then
ar rcs -o libprimes.a primes.o

I have now
libprimes.a

Test code
useprimes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;

extern "C++" int isprime(unsigned long long n);

int main(void)
{
	unsigned long long x;
	unsigned long long min=100000000000000000ull;
	unsigned long long max=100000000000000900ull;
	
std::	cout<<"Range from " << min <<"  to  " << max<<" \n";
std::	cout<<"  ";
    	for (x=min;x<max;x++)
	{
		if (isprime(x))
		{
std::		cout<<x<< "    Prime \n  ";
	}
}
	system("pause");
}  


To compile this I use
g++ -o useprimes.exe useprimes.cpp libprimes.a

Which of course gives me useprimes.exe

result
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Range from 100000000000000000  to  100000000000000900
  100000000000000003    Prime
  100000000000000013    Prime
  100000000000000019    Prime
  100000000000000021    Prime
  100000000000000049    Prime
  100000000000000081    Prime
  100000000000000099    Prime
  100000000000000141    Prime
  100000000000000181    Prime
  100000000000000337    Prime
  100000000000000339    Prime
  100000000000000369    Prime
  100000000000000379    Prime
  100000000000000423    Prime
  100000000000000519    Prime
  100000000000000543    Prime
  100000000000000589    Prime
  100000000000000591    Prime
  100000000000000609    Prime
  100000000000000669    Prime
  100000000000000691    Prime
  100000000000000781    Prime
  100000000000000787    Prime
  100000000000000817    Prime
  100000000000000819    Prime
  100000000000000871    Prime
  100000000000000889    Prime
  Press any key to continue . . .

 




I'll try that. (It looks the same as what I did.)

EDIT: It did not work, I get the same error.
Something about an index.
Last edited on
My g++ version is 8.1.0, 64 bits.
I keep the .a file in the same folder as the C++ test file.
Try omitting the
extern "C++"
in the test file.

I didn't think there would be a problem within g++, but using the lib with visual C++ maybe, but I cannot test (I don't have visual C++).
Did you use ar rcs to convert from .o to .a?

Try using the .o file only
g++ -o useprimes.exe useprimes.cpp primes.o
It works here.

Are your symbols present?
try
nm libprimes.a
Here are my results
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
C:\Users\Computer\Desktop\fb\C++\TESTERS>nm libprimes.a

primes.o:
0000000000000000 b .bss
0000000000000000 d .data
0000000000000000 p .pdata
0000000000000000 p .pdata$_ZSt4sqrtIyEN9__gnu_cxx11__enable_ifIXsrSt12__is_integerIT_E7__valueEdE6__typeES3_
0000000000000000 r .rdata
0000000000000000 r .rdata$zzz
0000000000000000 t .text
0000000000000000 t .text$_ZSt4sqrtIyEN9__gnu_cxx11__enable_ifIXsrSt12__is_integerIT_E7__valueEdE6__typeES3_
0000000000000000 r .xdata
0000000000000000 r .xdata$_ZSt4sqrtIyEN9__gnu_cxx11__enable_ifIXsrSt12__is_integerIT_E7__valueEdE6__typeES3_
0000000000000000 T _Z7isprimey
0000000000000000 T _ZSt4sqrtIyEN9__gnu_cxx11__enable_ifIXsrSt12__is_integerIT_E7__valueEdE6__typeES3_
                 U sqrt 


They have dll hell, which I think encompasses .a files.





I tried the following command:
nm library.a


And the error was:
library.o: file format not recognized.
Pages: 12