0

I have a rather complex build I'm trying to do, but I'll simplify it a little bit for this question. I have three c++ files (main.cpp file2.cpp and file3.cpp) that I am trying to compile and link against 3 static libs (libx.a liby.z libz.a) to produce an executable.

There are many dependencies involved.

All three c files are dependent on all 3 libs. libx is dependent on liby and libz. And finally, libx is also dependent on several callback functions contained in file2.cpp.

What command line would build this correctly? I have tried dozens of variations and nothing has satisfied the linker yet.

If it matters, the libs are pure c code compiled with gcc. Sources are c++ and I'm compiling/linking with g++. I have this working correctly as a visual studio project, and am trying to port to linux.

2 Answers 2

2

From your post:

g++ main.cpp file2.cpp file3.cpp -lx -ly -lz 

However, if static linking is causing you problems, or you need to distribute any of the libs, then you may consider making them shared objects (.so files, commonly called DSOs). In that case, when you build libx.a, for example, compile all the sources to object files, and then combine them with

g++ -shared *.o -o libx.so -ly -lz 

(this version assumes that liby.a and libz.a are stills static, and will be combined into libx.so

Sign up to request clarification or add additional context in comments.

6 Comments

and not to pollute the answer, but when you say "callback", are you passing a pointer of a function in file1.cpp, or have you directly referenced a function from file1.cpp in libx.a? The former presents no issues at all, the second is bad, and indicates (without knowing more) that file1.cpp should be part of libx.a
Radical, unfortunately it is the latter, and this solution isn't working for me.
I think this command line should still fit the bill. However, your situation isn't optimal (okay, really, it's bad software factorization). A possible solution is to split file1.cpp into those parts that need to be in libx.a, and leave the remainder separate. That's irrelevant to the answer, but may help your current situation.
I'm not in a position to refactor this code right now - it was created by my boss and passed on to me for porting and new feature addition. I do agree with you though. Not sure why this solution isn't working for me...I'll keep playing with it.
Ahh, that's an important point. It'd be better to make your libs into shared objects (.sos in Linux, for example).
|
0

You may need to use extern "C" { } in your .cpp files to include the header for the C libs.

See Including C Headers in C++ in How To Mix C and C++.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.