User forums > Help

How to Link to Static Lib Example?

<< < (2/2)

Game_Ender:
Here is how the command line should look:


--- Code: ---g++ -L"C:\static\lib\directory" main.o -o staticlibusr.exe  -lmystaticlib

--- End code ---

You would get this by adding "C:\static\lib\directory" to the linker directories (important).  Then adding "mystaticlib" to the link libraries.

mejohnsn:
Hi again y'all-

I just noticed that the final log output in my previous post is missing the reference to the library. Which certainly explains why _it_ fails. But it still does not explain why the previous attempt also failed, with a compiler command that had such a suspicous looking -L argument.

So now I restored the reference to the static library in (rt click on)Console Application:Build Options:Linker:Link Libraries, to finally get a command line that makes the most sense of all to me. But this too fails with the same error message:-(

The log output this time is:
Switching to target: default
mingw32-g++.exe    -L"D:\Program Files\CodeBlocks\lib" -o d:\MyCodeBlocksProjects\StaticLibEx\StaticLibusr.exe .objs\main.o        D:\MyCodeBlocksProjects\StaticLibEx\libstaticlib.a
.objs\main.o: In function `main':
D:/MyCodeBlocksProjects/StaticLibEx/main.cpp:10: undefined reference to `SampleAddInt(int, int)'

I say this "makes the most sense to me", because I now see quote marks in the -L command, and I see both main.o and libstaticlib.a listed as object files(?). But I do not know mingw so well as to know for sure that that syntax is correct: I was relying on C::B to make it unnecessary for me to learn that level of detail;)

Does this help clarify what went wrong?

TDragon:
Some thoughts:

Your command line should be either
mingw32-g++.exe -L"D:\Program Files\CodeBlocks\lib" -o d:\MyCodeBlocksProjects\StaticLibEx\StaticLibusr.exe .objs\main.o D:\MyCodeBlocksProjects\StaticLibEx\libstaticlib.a
or
mingw32-g++.exe -L"D:\Program Files\CodeBlocks\lib" -Ld:\MyCodeBlocksProjects\StaticLibEx -o d:\MyCodeBlocksProjects\StaticLibusr.exe .objs\main.o -lstaticlib

Both projects must be entirely in C (.c source file extension), or if the final executable is in C++ (.cpp source file extension) it must surround declarations of C functions with the extern "C" declaration.

mejohnsn:
Thanks, TDragon!

You wrote:

>Some thoughts:

>Your command line should be either
>mingw32-g++.exe -L"D:\Program Files\CodeBlocks\lib" -o >d:\MyCodeBlocksProjects\StaticLibEx\StaticLibusr.exe .objs\main.o >D:\MyCodeBlocksProjects\StaticLibEx\libstaticlib.a
>or
>mingw32-g++.exe -L"D:\Program Files\CodeBlocks\lib" -Ld:\MyCodeBlocksProjects\StaticLibEx -o >d:\MyCodeBlocksProjects\StaticLibusr.exe .objs\main.o -lstaticlib

This is what I now have.


>Both projects must be entirely in C (.c source file extension),

Now that is the surprise to me, since I was using the sample projects in Project:New Project, one of which is C++ and the other C.

> or if the final executable is in C++ (.cpp source file extension)

It is, as you can see from the source code I posted earlier.

>it must surround declarations of C functions >with the extern "C" declaration.

That explains the error message perfectly! Thanks you. The fog lifts at last.

TDragon:
You're welcome. :)

To explain further: most C++ compilers are "link backwards-compatible" with C, provided that you account for "name mangling". This is a process in C++ whereby the name and arguments of a function are combined to create a unique identifier for it. Surrounding declarations with extern "C" {} turns off name mangling to allow C functions and structures, which are not mangled by the C compiler, to be imported.

Navigation

[0] Message Index

[*] Previous page

Go to full version