-1

I have 3 classes - I denote those by firstClass,secondClass,thirdClass. My headers - firstClass.h, secondClass.h, thirdClass.h and sources firstClass.cpp, secondClass.cpp, thirdClass.cpp. In class thirdClass I create instance of firstClass and two instance of secondClass. In main.cpp I deamonize and create instance thirdClass.

I want to create static library of thirdclass and linking to main.cpp. firstClass and thirdClass used the same library libm.a

I created library step by step as following:

  1. g++ -c -I-/usr/include/ -I-/usr/lib/ -I-/home/projects/Learninig firstClass.cpp -lstdc++ -lm-o WsChannel.o -w -m32
  2. g++ -c -I-/usr/include/ -I-/usr/lib/ --I-/home/projects/Learninig secondClass -lstdc++ -o secondClass.o -w -m32
  3. g++ -c -I-/usr/include/ -I-/usr/lib/ --I-/home/projects/Learninig thirdClass.cpp -lstdc++ -lm -o thirdClass.o -w -m32
  4. ar rcs libLearning.a firstClass.o secondClass.o thirdClass.o
  5. g++ main.cpp -L. -lLearning -lm -o MnLearning.o -m32

Compiling was maked correctly without any errors, but when I execute program I have same error. I spent some hours on checking code, but I don't find bugs. So then maybe compiling was incorrect. I did this using some tutorial in web. If whatever was unclearly I am ready to more explain my question.

Edit: My error: segfault at 557400000045 ip 00005574bd509dcd sp 00007ffd9e887900 error 4 in MnLearning[5574bd4f2000+26000]

1 Answers1

1

The error is surely inside your own source code. Avoid undefined behavior in it, and be scared of UB.

Your use of -I- is strange, and probably wrong. I recommend removing it (and also, at first, remove the -m32 flag if your computer and distribution is 64 bits; work first to have your program run correctly on your laptop, then port it later to 32 bits Linux by adding the -m32 flag). You might use preprocessor options like -H to be shown what files are included.

I recommend building your library and your program with some build automation tool, such as GNU make or ninja.

Configure your build to compile with all warnings and debug info, i.e. using g++ -Wall -Wextra -g with GCC. Improve your source code to get no warnings. Then use the gdb debugger to understand the behavior of your program (and library).

So then maybe compiling was incorrect.

No, the compiler is probably good, and you should trust it.

The bug is very likely to be in your own code.

My error: segfault at 557400000045 ip 00005574bd509dcd sp 00007ffd9e887900 error 4 in MnLearning[5574bd4f2000+26000]

Segmentation fault is a symptom of some error in your own code (e.g. some buffer overflow, some bad pointer dereference, etc; or other kind of UB).

You might also use valgrind.

I spent some hours on checking code, but I don't find bugs.

You did not spend enough time (some bugs may take you weeks of work to be found), and you forgot to use the debugger, a very handy tool to help you understand the behavior of your program and find bugs in it. Be aware that programming is difficult, and don't be discouraged.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Before compiling on g ++ I try used Visual Studio 2017, but I abandoned. Maybe I should try in CodeBlocks on Windows? On linux I am new. –  Jan 04 '18 at 15:10
  • No, keep working on Linux. It is very developer- and student- friendly. You just need to learn more about how to use it wisely. And CodeBlocks is not a compiler, but an [IDE](https://en.wikipedia.org/wiki/Integrated_development_environment) which runs compilation commands (and hides them from you). You are right in compiling on Linux thru commands. – Basile Starynkevitch Jan 04 '18 at 15:11
  • 1
    Thanks for answer. Yesterday I saw for a moment my code, but today I found a mistake. Again thanks for suggestions and a bucket of cold water. –  Jan 05 '18 at 11:44