Unused classes removed - c++

I have the following code:
namespace rm {
namespace lib {
class Object {
public:
Object() {printf("Hi\n");}
};
}
}
Now I wanted to compile the class into an shared library using g++ -Wall -Wno-unused -Werror -fPIC -g -Iinclude/ -c -O0 -o object.o object.cpp and g++ --shared -o librm.so object.o. Compilation works with no problems, however after checking the resulting library with nm librm.so | c++filt, rm::lib::Object::Object() is nowhere to be found. GCC gives out no warning, even with -Wextra enabled.
G++ version is (Raspbian 4.9.2-10) 4.9.2

Code defined in class are implicitly marked as inline (and it is not used), and so not saved in file.

Related

problem with including fmt library in several cpp files

Good day!
I installed the fmt library in Ubuntu. Added it in my project
#include "fmt/core.h"
#include "fmt/format.h"
#include "fmt/format-inl.h"
to use fmt::format_int и fmt::format. I added library headers in several cpp files of my project. During linkage I got the mistake "multiple definition":
obj/container.o: In function fmt::v7::format_error::~format_error()': container.cpp:(.text+0x40e): multiple definition of fmt::v7::format_error::~format_error()'
obj/line.o:line.cpp:(.text+0x40e): first defined here
I've read something about this mistake. It is recommended to divide declaration and implementation in h and cpp files, to set some status to objects that causes mistake and so on. But all this recommendations imply editing of library (not my!) code!
What is wrong?
I do the following
compilation of files - one by one
g++ -std=c++11 -Wall -o obj/line.o -c /home/...//line.cpp
g++ -std=c++11 -Wall -o obj/container.o -c /home/...//container.cpp
g++ -std=c++11 -Wall -o obj/geometryObject.o -c /...//geometryObject.cpp
g++ -std=c++11 -Wall -o obj/model.o -c /home/...//model.cpp
g++ -std=c++11 -Wall -o obj/point.o -c /home/...//point.cpp
g++ -std=c++11 -Wall -o obj/main.o -c /home/...//main.cpp
Linking - error here
g++ -std=c++11 -Wall -o myapp obj/line.o obj/container.o obj/geometryObject.o obj/model.o obj/point.o obj/main.o
You shouldn't be including fmt/format-inl.h because it's an internal header. Please see the documentation for the list of public headers and what they provide.

LLD error on -O2 Function Scoped Static TLS cannot be used against symbol recompile with -fPIC

I am using LLVM's LLD8 to replace GNU-LD to improve link speed. But when I make .so file, there is a strange error in the release mode. After streamlining, I reproduced this problem with a simple example
test.h:
#ifndef TEST_H
#define TEST_H
inline int func(){
static __thread int i = 1;
return i;
}
void handle();
#endif
test.cpp
#include <iostream>
#include <stdio.h>
#include "test.h"
void handle() {
std::cout << func() << std::endl;
}
main.cpp
#include "test.h"
void x(){
handle();
}
following cmds copied from make V=1 stdout in my project, i save those cmds into a script and compile my example file
compile lib:
$dpath/bin/g++ -DLT_OBJDIR=\".libs/\" -I. -I$dpath/include/ -w -std=gnu++11 -g -O2 -D_GLIBCXX_USE_CXX11_ABI=0 -D_NO_EXCEPTION -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DNDEBUG -Wall -Werror -Wextra -Wunused-parameter -Wformat -Wconversion -Wno-deprecated -Wno-invalid-offsetof -finline-functions -fno-strict-aliasing -mtune=core2 -Wno-psabi -Wno-sign-compare -Wno-literal-suffix -DGCC_52 -DUSE_POSIX_FALLOCATE -DSUPPORT_SSE4_2 -DHAVE_SCHED_GETCPU -DHAVE_REALTIME_COARSE -DHAVE_FALLOCATE -c test.cpp -fPIC -DPIC -o .libs/test.o
link lib
ar cru .libs/lib.a .libs/test.o
ranlib .libs/lib.a
compile files
$dpath/bin/g++ -DLT_OBJDIR=\".libs/\" -I. -I$dpath/include -std=gnu++11 -g -O2 -D_GLIBCXX_USE_CXX11_ABI=0 -D_NO_EXCEPTION -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DNDEBUG -Wall -Werror -Wextra -Wunused-parameter -Wformat -Wconversion -Wno-deprecated -Wno-invalid-offsetof -finline-functions -fno-strict-aliasing -mtune=core2 -Wno-psabi -Wno-sign-compare -Wno-literal-suffix -DGCC_52 -DUSE_POSIX_FALLOCATE -DSUPPORT_SSE4_2 -DHAVE_SCHED_GETCPU -DHAVE_REALTIME_COARSE -DHAVE_FALLOCATE -c main.cpp -fPIC -DPIC -o .libs/main.o
link so
$dpath/bin/ld.lld -shared -nostdlib .libs/main.o -l:lib.a -L./.libs -L/usr/lib64 -L$glib/gcc/x86_64-unknown-linux-gnu/5.2.0 -L$glib/gcc -L$glib/../lib64 -L$glib -L$dpath/lib/libstdc++.so --allow-shlib-undefined -soname server.so.0 -o ./server.so.0.0.0
finally i got this error
ld.lld: error: can't create dynamic relocation R_X86_64_DTPOFF32 against symbol: func()::i in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
>>> defined in ./.libs/lib.a(test.o)
>>> referenced by test.cpp:6
>>> test.o:(handle()) in archive ./.libs/lib.a
as you seen, I have already pass -fPIC in my cmds, so i pass -znotext to lld
but i got another error
ld.lld: error: relocation R_X86_64_DTPOFF32 cannot be used against symbol func()::i; recompile with -fPIC
>>> defined in ./.libs/lib.a(test.o)
>>> referenced by test.cpp:6`enter code here`
>>> test.o:(handle()) in archive ./.libs/lib.a
This error only reported when I compile my project in release mode, when i remove -O2, it links well.
But when i use the so/bin which linked in debug mode to run testcases, i got a lot of unexcept core dumps around codes which use function scoped static tls.

Why is GCC not able to inline functions with -flto and profile guided optimization within a static library?

I'm using GCC version 4.7.2. I create a static library having two files "ctest1.cpp" and "ctest2.cpp".
ctest1.cpp
#include <stdio.h>
#include "ctest2.h"
void ctest1()
{
printf("In ctest1");
ctest2();
}
ctest2.cpp
#include <stdio.h>
void ctest2()
{
printf("In ctest2");
}
The header file "ctest2.h" is,
void ctest2();
And similarly the file "ctest1.h",
void ctest1();
This static library is linked to the following main file "in_test.cpp",
in_test.cpp
#include <stdio.h>
#include "ctest1.h"
using namespace std;
int main()
{
ctest1();
printf("InMain\n");
return 0;
}
I was expecting that after providing proper feedback and enabling -flto, the compiler should have inlined the function call ctest2() in file "ctest1.cpp"(and even call ctest1() in "in_test.cpp"), but it doesn't. Following are the compilation steps that I follow:
g++ -Wall -c -g -O3 -fprofile-generate -ftest-coverage ctest2.cpp ctest1.cpp
ar -rcsv libtest.a ctest2.o ctest1.o
g++ -Wall -g -O3 -fprofile-generate -ftest-coverage in_test.cpp -o checking libtest.a
For training I run the executable n times, then
g++ -Wall -c -g -O3 -flto -fwhole-program -fprofile-use ctest2.cpp ctest1.cpp
ar -rcsv libtest.a ctest2.o ctest1.o
g++ -Wall -g -O3 -flto -fwhole-program -fprofile-use in_test.cpp -o checking libtest.a
I have also tried -fuse-linker-plugin where I gave the path to the gold linker in the compilation command, but the functions were not inlined. I would also like to bring to your notice that when I try this experiment without creation of a static library (with flto and feedback), the compiler inlines the function calls. In that case, I just create one executable out of all the files. This is the reason I was expecting it to work in case of static libraries.
Can someone please tell me what's going wrong here ?

"Undefined reference" error when using custom destructor

I'm trying to write a class that needs to deallocate some memory, so I've defined a custom destructor. This is compiled as a shared library. However, when I try to compile a basic program to use that library, I'm getting the usual "undefined reference" error when a definition cannot be found. If I remove the destructor, this does not occur.
Here's a stripped-down example:
Header file:
#ifndef _SKYMAP_H_
#define _SKYMAP_H_
#include <vector>
#include "TCanvas.h"
class BL_Skymap {
public:
BL_Skymap();
~BL_Skymap();
protected:
TCanvas mCanvas;
};
#endif //_BENSLIBRARY_SKYMAP_H_
Source file:
\#include "BL_Skymap.h"
BL_Skymap::BL_Skymap()
{
}
BL_Skymap::~BL_Skymap()
{
}
Now the program I'm using is simply this:
\#include "BL_Skymap.h"
int main()
{
BL_Skymap map;
return(0);
}
Note that I'm using the ROOT analysis package (that's the TCanvas object). When I compile the small program above, I get the following errors (the Skymap class is compiled into libMyLibrary.so):
g++ test.cpp -o test -lMyLibrary `root-config --cflags --glibs`
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../lib/libMyLibrary.so: undefined reference to 'TCanvas::~TCanvas()'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../lib/libMyLibrary.so: undefined reference to 'TCanvas::TCanvas(bool)'
Please note that the root package gives a utility to generate the required compiler flags, which is the purpose of root-config --cflags --glibs above.
What am I missing here?
UPDATE: I've written a Makefile to perform the compilation of my library, which executes the following:
g++ -Wall -Wextra -ansi -pedantic --std=c++11 -Isrc -Ihdr -MM -MT 'obj/BL_Skymap.o' src/BL_Skymap.cpp -MF BL_Skymap.d `root-config --cflags --glibs`
g++ -Wall -Wextra -ansi -pedantic --std=c++11 -Isrc -Ihdr -fPIC -o obj/BL_Skymap.o -c src/BL_Skymap.cpp `root-config --cflags --glibs`
g++ -Wall -Wextra -ansi -pedantic --std=c++11 -shared obj/*.o -o libMyLibrary.so
UPDATE2: I found the problem - in the final step in the compilation above, I was forgetting to add the call to root-config and therefore libMyLibrary.so wasn't linking to the ROOT libraries like it should have.
As stated in an update to the original question, my problem was that I was using the relevant flags for the ROOT libraries when I was compiling the BL_Skymap.o object file, but not when I was linking the object file together to make the libMyLibrary.so file.
Adding the ROOT flags to this final step fixed the problem.

Have some problems linking with ACE lib by using g++

I am using ACE6.0.2 on a Suse 11 x86 machine.The version of gcc is 4.3 and ACE-TAO lib has been compiled with no problem.
The test I wanted to do is very simple but it just can't pass.
I have three files.
a1.h defines a class A.
class A
{
public:
A();
void print();
};
a1.cpp has a function invoking the method from ACE lib.
#include "a1.h"
#include <ace/Thread.h>
#include <iostream>
A::A(){}
void A::print()
{
long t=ACE_Thread::self();
std::cout<<t<<std::endl;
}
main.cpp invokes print() from class A
#include "a1.h"
int main()
{
A a;
a.print();
return 0;
}
The compiling command I used is:
1.generate a1.o with ACE_thread
g++ -c -fPIC -fno-strict-aliasing -fvisibility=hidden -fvisibility-inlines-hidden -O3 -ggdb -pthread -Wall -W -Wpointer-arith -pipe -D_GNU_SOURCE -I/opt/ACE_wrappers -DACE_HAS_VALGRIND -D__ACE_INLINE__ -I.. -Wl,-E -L/opt/ACE_wrappers/lib -L. -o a1.o a1.cpp
2.generate shared libT.so
g++ -pthread -Wl,-O3 -shared -o libT.so a1.o -Wl,-E -L/opt/ACE_wrappers -L. -L/opt/ACE_wrappers/lib -lACE -ldl -lrt
3.generate main.o
g++ -c -fno-strict-aliasing -fvisibility=hidden -fvisibility-inlines-hidden -O3 -ggdb -pthread -Wall -W -Wpointer-arith -pipe -D_GNU_SOURCE -I/opt/ACE_wrappers -DACE_HAS_VALGRIND -D__ACE_INLINE__ -I.. -Wl,-E -L/opt/ACE_wrappers/lib -L. -o main.o main.cpp -lACE -ldl -lrt
4.link and generate the executable file
g++ -fno-strict-aliasing -fvisibility=hidden -fvisibility-inlines-hidden -O3 -ggdb -pthread -Wall -W -Wpointer-arith -pipe -D_GNU_SOURCE -I/opt/ACE_wrappers -DACE_HAS_VALGRIND -D__ACE_INLINE__ -I.. -Wl,-E -L/opt/ACE_wrappers/lib -L. -o main main.o -lT -lACE -ldl -lrt
The problems occurred at step 4:
main.o: In function `main':
/main.cpp:5: undefined reference to `A::A()'
/main.cpp:6: undefined reference to `A::print()'
I am new to C++ under linux and don't understand why this happens. There must be something wrong with my compiling commands.Thanks for help in advance.
Well, you have just enabled -fvisibility=hidden flag which makes all symbols hidden by default. As a result, your class A is not visible to anything outside the shared library you have compiled.
There are two solutions:
Do not use -fvisibility=hidden flag. This will make all symbols visible by default.
Mark A class (and pretty much everything else that you want to access in your shared library from "the outside world") as public. This is compiler-specific and is usually done with macros. For more information on gcc, see GCC Visibility Wiki.
Just to make it clear, this issue is yours and yours only. It has absolutely nothing to do with Ace or any other library.
Hope it helps. Good Luck!
Thanks user1349058! After a lot of hours researching, luckily I found your comment and it works perfectly.
For example, the command I used when building ACE library 32-bits on Linux is:
$ make static_libs=1 buildbits=32 no_hidden_visibility=1
$ make install