Automake issue with linking - c++

I'm working on a library that links another library to use its static functions.
Everything was working perfectly until I switched from including all my code in the header file to sharing it between the .h and .cpp file.
I tried to use ./configure --prefix="<same path as before>" and then make install but ended up with a "/usr/bin/install: cannot stat .libs/<library name>.so.0.0.0': No such file or directory" error. (also, I did change my makefile to accommodate for the inclusion of the .cpp file)
Then, trying to compile my old library (which should've still worked since the linked library didn't make install correctly and was still technically using the older version) I get a pile of errors that seem to have to do with c++ itself. For example:
.../gcc/4.9.1/linux64/include/c++/4.9.1/bits/stl_bvector.h:354:13: error: expected ';' at end of member declaration
There are an absolute ton of errors similar to the one above, all having to do with issues in the path .../gcc/4.9.1/linux64/include/c++/4.9.1/
Has anyone run into this error before? Let me know if I need to provide any more information, it's just that I've never had this happen so I'm at a loss as to where to proceed. It's likely something simple I'm missing but the longer I stare at it the less it makes sense.
Thanks!

Update: I ended up solving the issue I was having. Basically, the .so file was not being accessed, so I used make clean and then the linked library compiled properly. Next, the issues with the base library was just an extra character within a header file that was causing the compiler to blow up with over 1500 lines of errors. After fixing that, it was able to link up properly and is working fine now.

Related

How can I link a library that contains conditional types/variables definition based on global variables defined through CMake?

Introduction
I am trying to use Toulbar2 as a C++ library in my CMake project, however I am having much trouble linking it to my main executable.
I found many similar questions on this topic, both here and on other similar website, but none of them helped me with my specific issue. I tried literally everything and I did not menage to make it work, I was hoping that some of you may help me with that.
I am running Ubuntu 18.04, CMake version 3.23 and in my project I am using the standard C++11. I am a proficient programmer, but I am just an beginner/intermediate user of both C++ and CMake.
What I've already tried to do
I cannot list all my attempts, so I will only mention those I think are my best ones, to give you an idea of what I may be doing wrong.
1) In my first attempt, I tried to use the same approach I used for any non-standard library I imported, i.e. using find_package() in CMakeLists.txt to then link the found LIBRARIES and include the found INCLUDE_DIRS. However, I soon realised that Toulbar2 provides neither a Find<package>.cmake or <name>Config.cmake file. So, this approach could not work.
2) My second attempt is the one that in my opinion brought me the closest to the solution I hoped for. You can easily compile Toulbar2 as a dynamic library using the command: cmake -DLIBTB2=ON .. in an hypothetical build directory you previously created. After compiling with make you have your .so file in build/lib/Linux. After installation, you can make CMake find this library by itself using the command find_library. So, my CMakeLists.txt ended up looking like this:
[...]
find_library(TB2_LIBRARIES tb2)
if(TB2_LIBRARIES)
set(all_depends ${all_depends} ${TB2_LIBRARIES})
else(TB2_LIBRARIES)
add_compile_definitions("-DNO_TB2")
message("Compiling without Toulbar2, if you want to use it, please install it first")
endif(TB2_LIBRARIES)
[...]
target_link_libraries(main ${all_depends})
[...]
This code works to some extent, meaning that CMake correctly finds the library and runs the linking command, however if I try to #include <toulbar2lib.hpp> the header is not found. So I figured out I should have told CMake where to find that header, so I ended up adding a
include_directories(/path/to/header/file's/directory)
However, I still have another problem. The header is found, but a lot of names used in the header are not found at compilation time. The reason is that in Toulbar2 some variables/types are defined conditionally by using preprocessing directives like #ifdef or #ifndef, and in turn the global variables used in these conditions are defined through CMake at compilation time. If you are interested in an example, I can mention the Cost type that is used in the mentioned header file. I see that there's a piece missing in the puzzle here, but I cannot figure out which one. Since I pre-compiled the library those definitions should exist when I include the header file, because I am correctly linking the correspondent library that contains those definitions.
3) My third attempt is less elegant than the the other two I mentioned, but I was desperately trying to find a solution. So, I copied the whole toulbar2 cloned folder inside my project and I tried to add it as a subdirectory, meaning that my main CMakeLists.txt contains the line:
add_subdirectory(toulbar2)
It provides a CMakeLists.txt too, there should be no problem in doing it. Then I include the src directory of toulbar2, that contains the header file I need, and I should be okay. Right? Wrong. I got the same problem that I had before with (2), i.e. some variables/types conditionally defined were not actually defined when I tried to compile my project, even though the subproject toulbar2 was correctly (no errors) compiled.
I just wanted to mention that any answer is welcome, however if you could help me figure out an elegant solution (see 1 or 2) for this problem it would be way better, as this code is intended to be published soon or later. Thank you in advance for your help.
Solution 2) looks fine. You just need to add the following compilation flags -DNDEBUG -DBOOST -DLONGDOUBLE_PROB -DLONGLONG_COST when compiling your project with toulbar2lib.hpp. See github/toulbar2 README.md how to compile without cmake for those flags (except WCSPFORMATONLY that should not by used in this context).

How C/C++ headerfiles are processed in vscode

I'm learning C now using vscode. I got curious and tried modifying the headerfiles. stdio.h to be specific and I found some really peculiar behavior. I studied the behavior using the simple Helloworld program.
infos: Windows 10 OS, VScode v1.56.0, Compiled using CL not gcc, ucrt file path:
C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt
The code:
#include<stdio.h>
int main()
{
printf("Hello world!");
}
So first I tried adding some random lines in the header file and save it. The code ran fine without any errors. I tried it after restarting. It still worked fine.
Then I commented the whole file with 2k+ and saved it and was expecting for the compiler to tell me that printf is not declared anywhere. But still again the code worked fine.
Then I reverted all changes, copied the header file to another folder and deleted the file in the ucrt folder
So. Now for the first time compiler said printf isn't declared anywhere.
I modified the headerfile outside and copied it to the urct folder and the code worked fine again.
So, as I checked into the file (in ucrt) the changes I made isn't showing here.
So I again deleted the headerfile in ucrt folder, cleared everyline added 2 3 random lines there saved it. And then pasted it in the ucrt to see that code still works really fine. And as I checked the header file.......It is the same old stdio.h that was when I installed it.
And Finally....when I copied the header file in ucrt and pasted it outside. And to my surprise It had the lines I added in No.5
There is only one explanation that I can think of...That VScode overrides any changes in the file.
So here are my questions:
How headerfiles are processed in vscode.
Can we actually modify it(The one in ucrt folder)
What is actually happening in the steps I mentioned above. Why these weird outcomes.
What is the difference between compiling with CL and with GCC.
NOTE: I know it's too lengthy I just wanted to give as much information I have.
VSCode itself doesn't do much with header files, the code explorer, intellisense and linter might have include paths but it is not responsible in any way for code-gen.
You can modify your system stdlib but I really wouldn't recommend it - my recommendation is to compile with -ffreestanding (on GCC and Clang) and then have a copy of a stdlib in your project and include it from there manually (Using -I or by using relative includes).
The most likely culprit is that your compiler & vscode's code explorer are being passed a flag which is modifying their include path, so the stdlib they are accessing is the system one, and the strange file modification behaviour can be attributed to how you are accessing the file (i.e. if you are using Go To Declaration).
Each may have a different default include setup, GCC uses the GNU Toolchain where it can - but ultimately they both follow the same preprocessing rules and its just a question of looking at what is included by default.
One last thing is that sometimes compilers will be nice and if stdio.h or the like isn't included and its a common or stdlib function, they will provide the correct prototype automatically and link as normal without the header being included - its not the case for my GCC config at least, and I'm not sure about CL

Stubbing the HAL library for STM32 Unit tests

To get unit tests working for my STM32 project I am mocking the Hal library.
So far we have managed to Mock it and create dummy files. However I am running into an issue.
At this point we have 3 .h files, one for #defines, one for functions and the other is a straight copy of a HAL file. The program seems to compile and is giving me linking errors.
The error it is giving is "Multiple definition of", but not one error. Like roughly 200. It is giving an error on all the functions in hal_stub_functions.h
It even seems to loop a couple of times. We have checked all files. None include the wrong files or each other. All files contain If/Def/Endif. The 3 files do not include each other. The functions file includes the #Defines file.
I have no idea where to start looking for this. Tried the general stuff, as mentioned checking includes. Project settings are all correct.
If anybody has any help or ideas id greatly appreciate it.
EDIT:
Think I may have found something weird.
Image here
Seems like Eclipse is finding every function twice. On the same spot. How do i fix this?
EDIT 2:
I think the general problem is that the linker is somehow finding files twice.
Thanks in advance
:)
Solution:
Make all functions static.
As multiple files were including the .h file they were constantly redefining the functions. Even tho #ifdef was applied. #pragma once did not help either.

How to include Boost.DLL into C++ code?

I am trying to complete the Boost.DLL tutorial (http://www.boost.org/doc/libs/master/doc/html/boost_dll/tutorial.html)
However, I'm not able to even compile their source code, I keep getting:
fatal error: boost/dll/import.hpp: No such file or director
I tried manually copying the header files from the repository (https://github.com/apolukhin/Boost.DLL), but even when I copy these into "/usr/include/boost/" I get new errors from the same kind about "boost/predef/os.h".
I've tried reinstalling the complete boost library and everything, but without success.
Since they describe themselves as "Boost.DLL is a header only library. To start with the library you only need to include header"
It seems like there should be an easier way to fix this than go through the include-errors and manually search for the right files on the internet...

Need help understanding C++ libraries, compiling, linking, header files for specific project

This is my first stab at C++, also I know that the question is broad but I have a specific example that I'm working with so hopefully that will narrow everything down a bit.
I'm basically attempting to compile a C++ game manually in Linux (Ubuntu 14.04). The source code I am attempting to compile is located in this directory: https://github.com/akadmc/SmashBattle/tree/master/battle.
I'm CD'ing into the battle directory and, perhaps naively running
gcc *.cpp
I started seeing multiple issues as such:
compilation terminated.HealthPowerUp.cpp:1:21:
fatal error: SDL/SDL.h: No such file or directory #include "SDL/SDL.h"
and
compilation terminated.LaserBeamPowerUp.cpp:1:21:
fatal error: SDL/SDL.h: No such file or directory #include <SDL/SDL.h>
After researching header file includes I concluded that includes without <>'s are basically just relative paths to include a header file, and that when they are wrapped in <>'s they can either lookup the file through a listing of directories specified in an enviornment variable, or a command line option.
So my first question is, is there any reason the developer used
#include "SDL/SDL.h
AND
#include <SDL/SDL.h>
in different files? There was no SDL directory in the source code...
After realizing that SDL was missing from the source code / environment in one way or another I did tinkering. I was pretty confused (and still am) because I downloaded the SDL source files, didn't see any header files, ended up building a version of SDL by using cmake, and then build. I realized afterwards that I just made a local executable and didn't yield any header files. Then I realized that I just needed the development library, downloaded that, and put higher in the directory tree and then included it at compile with
c++ *.cpp -I $HOME/Desktop/smashProject/source/
Afterwards, the previous header file errors went away - but I started getting errors like the following:
Text.cpp:(.text+0x17): undefined reference to `SDL_RWFromFile'
Text.cpp:(.text+0x24): undefined reference to `SDL_LoadBMP_RW'
Text.cpp:(.text+0x34): undefined reference to `SDL_DisplayFormat'
And so on. Am I generally headed in the right path or do I have some misunderstanding about compiling, including development libraries, etc? Also I've read the the order of the compilation matters, and I'm not using any order + the developer didn't put a makefile in the source code or anything. I'm generally just confused as to how I should be doing this. Any help would be greatly appreciated.
Yes, you are on the right track. However, now you need to have a linkage to the SDL libraries. The -I just includes an extra library path but you have to actually link your assembly to the SDL files.
See this stack overflow question for more information.
How to compile an example SDL program written in C?