Making a Static Lib in Linux

Started by
6 comments, last by jamesleighe 11 years, 2 months ago

(c++, clang++, debian: wheezy)

I'm trying to build and link to a static library in Linux (which I have never programmed in before).

The problem I am having is that when I try to link to my library it tells me:

"../libsophia/build/libsophia.a: could not read symbols: Archive has no index; run ranlib to add one"

Then, when I try to run "nm libsophia.a" I get:

nm: sa_math.o: File format not recognized
nm: sa_sophia.o: File format not recognized
nm: sa_stringtools.o: File format not recognized
nm: sa_string.o: File format not recognized
nm: sa_baseapplication.o: File format not recognized
nm: sa_random.o: File format not recognized
So, I must at least be compiling it wrong!
Here are my makefiles:
(My Static Library)

	clang_compile = clang++ -std=c++0x -S

all:
	$(clang_compile) sa_math.cpp		-o build/sa_math.o
	$(clang_compile) sa_sophia.cpp		-o build/sa_sophia.o
	$(clang_compile) sa_stringtools.cpp	-o build/sa_stringtools.o
	$(clang_compile) sa_string.cpp		-o build/sa_string.o
	$(clang_compile) sa_baseapplication.cpp	-o build/sa_baseapplication.o
	$(clang_compile) sa_random.cpp		-o build/sa_random.o

	ar rcs build/libsophia.a		\
		build/sa_math.o			\
		build/sa_sophia.o		\
		build/sa_stringtools.o		\
		build/sa_string.o		\
		build/sa_baseapplication.o	\
		build/sa_random.o

(My Test Application)


clang_compile = clang++ -std=c++0x -I ../libsophia/public/ -l:../libsophia/build/libsophia.a

all:
	$(clang_compile) main.cpp		-o build/main

I want to mention I know nothing about writing software in Linux, and any help would be much appreciated!

Thank you!

Advertisement

The S option is likely giving you assembly text output. Use -c instead.

New C/C++ Build Tool 'Stir' (doesn't just generate Makefiles, it does the build): https://github.com/space222/stir

OK! We made some progress, now "nm" gives the proper results.

However, I'm still getting the same amount of undefined references somehow. Am I linking to the lib properly in the "clang++" call?

I should also mention that this code is working in windows, so I'm pretty sure it's not a code error.

Try "-L../libsophia/build -lsophia" (without the quotes).

Hmm, still no dice it would seem.

Here is how I compile the sophia static library:


clang++ -std=c++0x -c sa_math.cpp		-o build/sa_math.o
clang++ -std=c++0x -c sa_sophia.cpp		-o build/sa_sophia.o
clang++ -std=c++0x -c sa_stringtools.cpp	-o build/sa_stringtools.o
clang++ -std=c++0x -c sa_string.cpp		-o build/sa_string.o
clang++ -std=c++0x -c sa_baseapplication.cpp	-o build/sa_baseapplication.o
clang++ -std=c++0x -c sa_random.cpp		-o build/sa_random.o
In file included from sa_random.cpp:4:
In file included from ./SFMT-src-1.4/SFMT.h:67:
./SFMT-src-1.4/SFMT-params.h:7:4: warning: #warning "SFMT_MEXP is not defined. I assume MEXP is 19937." [-W#warnings]
  #warning "SFMT_MEXP is not defined. I assume MEXP is 19937."
   ^
1 warning generated.
ar rcs build/libsophia.a		\
		build/sa_math.o			\
		build/sa_sophia.o		\
		build/sa_stringtools.o		\
		build/sa_string.o		\
		build/sa_baseapplication.o	\
		build/sa_random.o

Here is what happens when I attempt to compile the test application:


clang++ -std=c++0x -I ../libsophia/public/ -L ../libsophia/build -lsophia  main.cpp		-o build/main
/tmp/main-bh2HIY.o: In function `main':
main.cpp:(.text+0x48e): undefined reference to `sa::Random::Random()'
main.cpp:(.text+0x49c): undefined reference to `sa::Random::Seed(unsigned int)'
main.cpp:(.text+0x4c8): undefined reference to `sa::Random::GetFloat64(double, double)'
main.cpp:(.text+0x4f9): undefined reference to `sa::Random::GetFloat64(double, double)'
main.cpp:(.text+0x52a): undefined reference to `sa::Random::GetFloat64(double, double)'
main.cpp:(.text+0x57a): undefined reference to `sa::String::GetCString() const'
main.cpp:(.text+0x5bd): undefined reference to `sa::String::~String()'
main.cpp:(.text+0x5dd): undefined reference to `sa::Random::~Random()'
main.cpp:(.text+0x617): undefined reference to `sa::String::~String()'
main.cpp:(.text+0x629): undefined reference to `sa::Random::~Random()'
/tmp/main-bh2HIY.o: In function `sa::Vector3<float>::GetString(int) const':
main.cpp:(.text._ZNK2sa7Vector3IfE9GetStringEi[_ZNK2sa7Vector3IfE9GetStringEi]+0x27): undefined reference to `sa::String::String()'
main.cpp:(.text._ZNK2sa7Vector3IfE9GetStringEi[_ZNK2sa7Vector3IfE9GetStringEi]+0x4e): undefined reference to `sa::String::Copy(float const*, unsigned int, unsigned int)'
main.cpp:(.text._ZNK2sa7Vector3IfE9GetStringEi[_ZNK2sa7Vector3IfE9GetStringEi]+0x82): undefined reference to `sa::String::~String()'
main.cpp:(.text._ZNK2sa7Vector3IfE9GetStringEi[_ZNK2sa7Vector3IfE9GetStringEi]+0x90): undefined reference to `sa::String::~String()'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [all] Error 1

I'm only testing it by instantiating an instance of the Random class and using it to fill a Vector3 (which is a template and so has no linking issues).

String is popping up because I try and get a String of the Vector's values to print out.

So I think everything that could be undefined is undefined.

Try putting the library stuff last on the command line.

New C/C++ Build Tool 'Stir' (doesn't just generate Makefiles, it does the build): https://github.com/space222/stir

That helped!

Now I just have multiple definition issues. But at least that's more familiar territory.


james@debian:~/Dropbox/Fly Games/project_sophia/test$ clang++ -std=c++0x -I ../libsophia/public/ main.cpp -o build/main -L ../libsophia/build -lsophia
../libsophia/build/libsophia.a(sa_string.o): In function `sa::MemShiftForward(void*, unsigned int, unsigned int, unsigned int)':
sa_string.cpp:(.text+0x160): multiple definition of `sa::MemShiftForward(void*, unsigned int, unsigned int, unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x1a0): first defined here
../libsophia/build/libsophia.a(sa_string.o): In function `sa::MemShiftBackward(void*, unsigned int, unsigned int, unsigned int)':
sa_string.cpp:(.text+0x1c0): multiple definition of `sa::MemShiftBackward(void*, unsigned int, unsigned int, unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x200): first defined here
../libsophia/build/libsophia.a(sa_string.o): In function `sa::Free(void*)':
sa_string.cpp:(.text+0x80): multiple definition of `sa::Free(void*)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0xc0): first defined here
../libsophia/build/libsophia.a(sa_string.o): In function `sa::Calloc(unsigned int)':
sa_string.cpp:(.text+0x20): multiple definition of `sa::Calloc(unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x60): first defined here
../libsophia/build/libsophia.a(sa_string.o): In function `sa::Malloc(unsigned int)':
sa_string.cpp:(.text+0x0): multiple definition of `sa::Malloc(unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x40): first defined here
../libsophia/build/libsophia.a(sa_string.o): In function `sa::MemSet(void*, unsigned char, unsigned int)':
sa_string.cpp:(.text+0xa0): multiple definition of `sa::MemSet(void*, unsigned char, unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0xe0): first defined here
../libsophia/build/libsophia.a(sa_string.o): In function `sa::MemCopy(void const*, void*, unsigned int)':
sa_string.cpp:(.text+0xe0): multiple definition of `sa::MemCopy(void const*, void*, unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x120): first defined here
../libsophia/build/libsophia.a(sa_string.o): In function `sa::MemMove(void*, void*, unsigned int)':
sa_string.cpp:(.text+0x120): multiple definition of `sa::MemMove(void*, void*, unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x160): first defined here
../libsophia/build/libsophia.a(sa_string.o): In function `sa::Realloc(void*, unsigned int)':
sa_string.cpp:(.text+0x50): multiple definition of `sa::Realloc(void*, unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x90): first defined here
../libsophia/build/libsophia.a(sa_string.o): In function `unsigned int samath::RoundUpToPowerOfTwo<unsigned int>(unsigned int)':
sa_string.cpp:(.text+0x210): multiple definition of `unsigned int samath::RoundUpToPowerOfTwo<unsigned int>(unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x250): first defined here
../libsophia/build/libsophia.a(sa_string.o): In function `double samath::Random<double>(double, double)':
sa_string.cpp:(.text+0x360): multiple definition of `double samath::Random<double>(double, double)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x3a0): first defined here
../libsophia/build/libsophia.a(sa_string.o): In function `float samath::Random<float>(float, float)':
sa_string.cpp:(.text+0x290): multiple definition of `float samath::Random<float>(float, float)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x2d0): first defined here
../libsophia/build/libsophia.a(sa_random.o): In function `sa::MemShiftForward(void*, unsigned int, unsigned int, unsigned int)':
sa_random.cpp:(.text+0x160): multiple definition of `sa::MemShiftForward(void*, unsigned int, unsigned int, unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x1a0): first defined here
../libsophia/build/libsophia.a(sa_random.o): In function `sa::MemShiftBackward(void*, unsigned int, unsigned int, unsigned int)':
sa_random.cpp:(.text+0x1c0): multiple definition of `sa::MemShiftBackward(void*, unsigned int, unsigned int, unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x200): first defined here
../libsophia/build/libsophia.a(sa_random.o): In function `sa::Free(void*)':
sa_random.cpp:(.text+0x80): multiple definition of `sa::Free(void*)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0xc0): first defined here
../libsophia/build/libsophia.a(sa_random.o): In function `sa::Calloc(unsigned int)':
sa_random.cpp:(.text+0x20): multiple definition of `sa::Calloc(unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x60): first defined here
../libsophia/build/libsophia.a(sa_random.o): In function `sa::Malloc(unsigned int)':
sa_random.cpp:(.text+0x0): multiple definition of `sa::Malloc(unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x40): first defined here
../libsophia/build/libsophia.a(sa_random.o): In function `sa::MemSet(void*, unsigned char, unsigned int)':
sa_random.cpp:(.text+0xa0): multiple definition of `sa::MemSet(void*, unsigned char, unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0xe0): first defined here
../libsophia/build/libsophia.a(sa_random.o): In function `sa::MemCopy(void const*, void*, unsigned int)':
sa_random.cpp:(.text+0xe0): multiple definition of `sa::MemCopy(void const*, void*, unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x120): first defined here
../libsophia/build/libsophia.a(sa_random.o): In function `sa::MemMove(void*, void*, unsigned int)':
sa_random.cpp:(.text+0x120): multiple definition of `sa::MemMove(void*, void*, unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x160): first defined here
../libsophia/build/libsophia.a(sa_random.o): In function `sa::Realloc(void*, unsigned int)':
sa_random.cpp:(.text+0x50): multiple definition of `sa::Realloc(void*, unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x90): first defined here
../libsophia/build/libsophia.a(sa_stringtools.o): In function `sa::MemShiftForward(void*, unsigned int, unsigned int, unsigned int)':
sa_stringtools.cpp:(.text+0x160): multiple definition of `sa::MemShiftForward(void*, unsigned int, unsigned int, unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x1a0): first defined here
../libsophia/build/libsophia.a(sa_stringtools.o): In function `sa::MemShiftBackward(void*, unsigned int, unsigned int, unsigned int)':
sa_stringtools.cpp:(.text+0x1c0): multiple definition of `sa::MemShiftBackward(void*, unsigned int, unsigned int, unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x200): first defined here
../libsophia/build/libsophia.a(sa_stringtools.o): In function `sa::Free(void*)':
sa_stringtools.cpp:(.text+0x80): multiple definition of `sa::Free(void*)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0xc0): first defined here
../libsophia/build/libsophia.a(sa_stringtools.o): In function `sa::Calloc(unsigned int)':
sa_stringtools.cpp:(.text+0x20): multiple definition of `sa::Calloc(unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x60): first defined here
../libsophia/build/libsophia.a(sa_stringtools.o): In function `sa::Malloc(unsigned int)':
sa_stringtools.cpp:(.text+0x0): multiple definition of `sa::Malloc(unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x40): first defined here
../libsophia/build/libsophia.a(sa_stringtools.o): In function `sa::MemSet(void*, unsigned char, unsigned int)':
sa_stringtools.cpp:(.text+0xa0): multiple definition of `sa::MemSet(void*, unsigned char, unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0xe0): first defined here
../libsophia/build/libsophia.a(sa_stringtools.o): In function `sa::MemCopy(void const*, void*, unsigned int)':
sa_stringtools.cpp:(.text+0xe0): multiple definition of `sa::MemCopy(void const*, void*, unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x120): first defined here
../libsophia/build/libsophia.a(sa_stringtools.o): In function `sa::MemMove(void*, void*, unsigned int)':
sa_stringtools.cpp:(.text+0x120): multiple definition of `sa::MemMove(void*, void*, unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x160): first defined here
../libsophia/build/libsophia.a(sa_stringtools.o): In function `sa::Realloc(void*, unsigned int)':
sa_stringtools.cpp:(.text+0x50): multiple definition of `sa::Realloc(void*, unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x90): first defined here
../libsophia/build/libsophia.a(sa_stringtools.o): In function `unsigned int samath::RoundUpToPowerOfTwo<unsigned int>(unsigned int)':
sa_stringtools.cpp:(.text+0x210): multiple definition of `unsigned int samath::RoundUpToPowerOfTwo<unsigned int>(unsigned int)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x250): first defined here
../libsophia/build/libsophia.a(sa_stringtools.o): In function `double samath::Random<double>(double, double)':
sa_stringtools.cpp:(.text+0x360): multiple definition of `double samath::Random<double>(double, double)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x3a0): first defined here
../libsophia/build/libsophia.a(sa_stringtools.o): In function `float samath::Random<float>(float, float)':
sa_stringtools.cpp:(.text+0x290): multiple definition of `float samath::Random<float>(float, float)'
/tmp/main-lSPKsO.o:main.cpp:(.text+0x2d0): first defined here
../libsophia/build/libsophia.a(sa_random.o): In function `sa::Random::Seed(unsigned int)':
sa_random.cpp:(.text+0x29a): undefined reference to `sfmt_init_gen_rand'
../libsophia/build/libsophia.a(sa_random.o): In function `sfmt_genrand_uint32':
sa_random.cpp:(.text+0x2fd): undefined reference to `sfmt_gen_rand_all'
../libsophia/build/libsophia.a(sa_random.o): In function `sfmt_genrand_uint64':
sa_random.cpp:(.text+0x3d9): undefined reference to `sfmt_gen_rand_all'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

It could be I made a mistake when moving over form "#pragma once" to the ifndef method somewhere but I tried to be careful and like I said in MSVC it builds without a warning.

Progress!

I fixed it!

It was because I had a define that used __inline (from MSVC) and clang was just ignoring it.

Thanks for all your help guys.

This topic is closed to new replies.

Advertisement