Platformio Linker Error When Including <functional> - c++

I'm using PlatformIO (with the VS Code extension), on Windows 10, and am compiling for the Teensy 3.5 board.
I've reduced a linker error in one of my projects to the following piece of code:
#include <Arduino.h>
#include <functional>
void setup()
{
// Serial.println();
std::function<void()> func;
func();
}
void loop() {}
When I try to compile and link this, it compiles fine, but fails to link. If i uncomment the Serial.println(), then it links and compiles.
I am using the following settings in the platformio.ini file:
[env:teensy35]
platform = teensy
board = teensy35
framework = arduino
The error it gives when failing to link is:
c:/users/zackh/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/5.4.1/../../../../arm-none-eabi/lib/armv7e-m/fpu\libc.a(lib_a-writer.o): In function _write_r':
writer.c:(.text._write_r+0x12): undefined reference to_write'
collect2.exe: error: ld returned 1 exit status
Any idea what's happening here?

The solution I found was to include the following line in the platformio.ini project.
build_flags = -llibc -lc

Related

Compiling multiple files in Visual Studio Code resulting in error

I'm relatively new to coding in C++ and have started working with main and header files and I've created a program to test it out, however, the following program results in the following compiler error:
Undefined symbols for architecture x86_64:
"Print()", referenced from:
_main in test-7d0225.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
This is my current code.
test.cpp
#include <iostream>
#include "test.h"
using namespace std;
int main() {
Print();
return 0;
}
test.h
#ifndef TEST_H
#define TEST_H
void Print();
#endif
test1.cpp
#include <iostream>
#include "test.h"
using namespace std;
void Print() {
cout << "Hello" << endl;
}
Nothing I've found online has helped me and my only assumptions are that my compiler isn't set up correctly. I've tried compiling both of these of files by typing in "g++ test.cpp test1.cpp", as well, but yields similar results. I would like to note that I am on Mac as well. Please feel free to leave any comments or suggestions for how I've asked this question, this is my first time on stack overflow.
Your code seems just fine, I’ll will try to compile it on vscode myself.
One problem could be your vscode launch.json. I have had the same problem as what I am describing. Vscode can be quite “Finicky” with low level languages like c and c++. In your launch file (or tasks file if it calls something in tasks) make sure you compile *.cpp files in the folder. I would look something like {folder}/**.cpp. To find the exact command look on vscode’s official site.
Given that you tried compiling your code outside of vscode and assuming that everything is in the same folder, it might be a coding problem. Again, your code looks fine. Go online and find some learning to code website with multiple file coding. Copy their code and do the same thing that you did with your code. If it doesn’t work there is definitely a problem with how to code is being compiled. Otherwise revise your code and compare to find the problem.
Also, could you provide the terminal command vscode prints (I am also on Mac). It will be in the terminal tab in the g++ section, not in bash)
P.S. I am also new to stack overflow so any suggestions you have for me will be great.

Cannot compile file using catch2 CATCH_CONFIG_MAIN

I've started out learning how to use Catch2 for testing my C++ code and am trying to set up a simple test.
My folder structure consists of three files all in the same folder:
catch.cpp //this is the catch_amalgamated.cpp file from GitHub
catch.hpp //this is the catch_amalgamated.hpp file from GitHub
test.cpp //this is my test file
All I have written in test.cpp is:
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
When I try to compile test.cpp I get the following error which I think indicates that there is no main() function found(?):
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
When I add a nominal main() function, the file compiles successfully, but as I understand it #define CATCH_CONFIG_MAIN is supposed to create the main() function for you so something clearly isn't working.
Can anyone shed any light on this?
The error was occurring because the default branch that google takes you to is the development branch of catch2, so I was using version 3 files and following version 2 documentation (as it has not been updated yet). Once I downloaded the v.2 file everything started working fine.

"undefined reference to `vkCreateInstance#12'" error when compiling C++ using Vulkan

I am trying to learn C++ and Vulkan while using windows 10. I have created a small program that I am compling with minGW. However when I add vkCreateInstance(&instanceinfo, nullptr, &myvulkaninstance) i get an error:
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\%USERNAME%\AppData\Local\Temp\ccDoOvlg.o:Renderer.cpp:(.text+0x5b): undefined reference to 'vkCreateInstance#12'
collect2.exe: error: ld returned 1 exit status
I get the above error in the 'Renderer.cpp' in the 'InitializeInstance' method (code can be seen below).
To compile my project I use this line in CMD g++ -L "C:/VulkanSDK/1.2.154.1/Lib/vulkan-1.lib" main.cpp Renderer.h Renderer.cpp -o build.exe. I do not know if I am compiling the library for Vulkan correctly as I was unable to find any good documentation about that online.
main.cpp
#include <iostream>
#include "Renderer.h"
using namespace std;
int main() {
Renderer renderer;
return 0;
}
Renderer.cpp
#include <iostream>
#include <cstdlib>
#include "Renderer.h"
// I again don't know if this is the right way to include this header
#include "C:/VulkanSDK/1.2.154.1/Include/vulkan/vulkan.h"
using namespace std;
Renderer::Renderer(){
}
void Renderer::InitializeInstance(){
VkInstanceCreateInfo instanceCreateInfo {};
instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
// This is the line where i get the error
auto error = vkCreateInstance(&instanceCreateInfo, nullptr, &vulkanInstance); // <--
if(VK_SUCCESS != error){
std::exit(-1);
}
}
Renderer::~Renderer(){
}
I have been using OpenGL and java (using LWJGL) for a while but now I wanted to learn Vulkan and C++. I am however still learning so I don't know a lot about any of these subjects. If you know how I could improve my compiling and/or prevent this error, please let me know.
Is your operating system 64-bit? If it is, please download MSYS2 and uninstall 32-bit MinGW. In MSYS2, use clang64 toolchain to compile your code again. It's not recommended to use 32-bit compilers with Vulkan SDK.

My Class.cpp is not being seen by the linker

I am trying to write a C++ class in a separate header and cpp file using VS Code as my IDE with the 'run' and 'C++' extensions.
main.cpp
#include "Fan.h"
int main() {
Fan fan1;
return 0;
}
Fan.h
#ifndef FAN_H
#define FAN_H
class Fan {
public:
Fan();
};
#endif
Fan.cpp
#include "Fan.h"
#include <iostream>
using namespace std;
Fan::Fan() {
cout << "Fan Class" << endl;
}
I really can't seem to find anything popping out as obviously wrong. I am wondering if it is a setup problem with VS Code.
If I change #include "Fan.h" in main.cpp to "Fan.cpp" it works so it makes me think that the code works but that the linker is not setup right.
Would appreciate any help!
EDIT: Ok so I've tried the code in a different IDE and it worked. It's something to do with VS Code. Here is the error:
[Running] cd "c:\Users\<USER>\Desktop\Fan\" && g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile && "c:\Users\<USER>\Desktop\Fan\"tempCodeRunnerFile
C:\Users\<USER>\AppData\Local\Temp\cclMFKxO.o:tempCodeRunnerFile.cpp:(.text+0x57): undefined reference to `Fan::Fan()'
collect2.exe: error: ld returned 1 exit status
It sounds like the IDE is only compiling main.cpp. You need to find the command that is compiling main.cpp and ensure that it also compiles fan.cpp into fan.obj. You will also need to ensure that both main.obj and fan.obj are passed to the linker (which produces the executable program, main.exe or whatever).
There are two steps involved here:
cpp -> obj (compiling each source file into a matching object file)
obj -> exe (linking many object files into an executable)
I would say to make a CMakeLists.txt file and add main.cpp and fan.cpp into the add_executable section. Then VS can handle and run files through CMake.

How to get Xlib code work in eclipse for C++ ubuntu

I have some code with the following headers for Xlib but don't know what to do to make it work...I can't find these headers.
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
Sorry new to this..
Okay so now I can view the headers in the foldrrs in my eclipse IDE.
and i get the following errors.Thee is no problem with the source as friends have run it from the terminal,and i want to do it from eclipse ide:
In function main':
/home/abbas/workspace/test3/Debug/../src/test3.cpp:51: undefined reference toXOpenDisplay'
and many other errors of the same type but the functions are different.
collect2: ld returned 1 exit status
make: *** [test3] Error 1
Plzz hellpp!!
Right click on Project Folder> Properties> C/C++ Build > Settings > GCC C++ Linker > Libraries > add "X11"