waf at linking time: "undefined reference" error - c++

So I've been banging my head on this compiler error for the last 2 hours and thought I would post the code here to see if anyone can shed any light on my mistake.
I have stripped out all the irrelevant bits to leave a bare minimal program (shown below) which as far as I can see should compile and run. If I comme but I can't see what is wrongnt out the call to testFunc in main then everything compiles and runs fine. With the call to testFunc in however, I get the following:
$ ./waf -v --run abr-tool
Waf: Entering directory `/home/conor/workspace/msc/AdaptiveIPTV/Software/conor/ns3/ns-3.15/build'
[1665/1822] cxxprogram: build/src/abr-tools/examples/abr-tool.cc.4.o -> build/src/abr-tools/ns3.15-abr-tool-debug
19:04:19 runner ['/usr/bin/g++', '-L/usr/lib', '-lboost_iostreams', '-L/usr/lib', '-lboost_iostreams', '-pthread', 'src/abr-tools/examples/abr-tool.cc.4.o', '-o', '/home/conor/workspace/msc/AdaptiveIPTV/Software/conor/ns3/ns-3.15/build/src/abr-tools/ns3.15-abr-tool-debug', '-Wl,-Bstatic', '-Wl,-Bdynamic', '-Wl,--no-as-needed', '-L.', '-L.', '-L.', '-L.', '-L.', '-L.', '-lns3.15-point-to-point-debug', '-lns3.15-internet-debug', '-lns3.15-mpi-debug', '-lns3.15-bridge-debug', '-lns3.15-network-debug', '-lns3.15-core-debug', '-lrt']
src/abr-tools/examples/abr-tool.cc.4.o: In function `main':
/home/conor/workspace/msc/AdaptiveIPTV/Software/conor/ns3/ns-3.15/build/../src/abr-tools/examples/abr-tool.cc:7: undefined reference to `testFunc()'
collect2: ld returned 1 exit status
As you can see the code below is being built as part of a much larger project, and I am aware that the error may be coming from that build process rather than an issue with my code, but either way I have hit something of a wall in my understanding of what is happening here. I'm learning c++ as I go and to tell the truth I don't feel experienced enough to even just compile this code on its own and be able to say "that should definitely work but it doesn't", which is why I present it like this.
Another couple of points which might be relevant:
I can use macros defined in abr-helper.h from abr-tools.cc, and the problem persists when I put abr-tools.cc in the same folder as abr-helper.h and just use '#include "abr-helper.h"'.
the original error was the same thing, but for a bunch of other stuff defined in abr-helper.h and use in abr-tools.cc
I would appreciate any help you folks can offer, thanks in advance.
abr-helper.h:
#ifndef ABR_HELPER_H
#define ABR_HELPER_H
#include <iostream>
void testFunc();
#endif /* ABR_HELPER_H */
abr-helper.cc:
#include <iostream>
#include "abr-helper.h"
void testFunc(){
std::cout << "this is all testFunc() does ..." << std::endl;
}
abr-tool.cc:
#include <iostream>
#include "ns3/abr-helper.h"
int main (int argc, char *argv[]){
std::cout << "in main()" << std::endl;
testFunc();
return 0;
}

It appears the file abr-helper.cc doesn't get compiled. You can easily test this by adding a
#error "Test"
line to that file. If the build succeeds, the file isn't getting compiled and you need to add it. How you do that depends on your compiler or IDE.

You need to include abr-helper.cc in your compilation, otherwise the implementation is not linked in.

I don't know waf, so I won't be able to give a solution. But here is what happens :
During compilation, everything works fine. The problem occurs during the linking (ld error).
On the line starting with : 19:04:19 runner, there is no reference to abr-helper.o or something like that. You must have forgotten to add abr-helper.cc somewhere in your waf configuration.

Additionally to the other answers (I also don't see the file abr-tool.cc to be compiled), the function void testFunc needs to be declared extern.

Related

G++ linker exiting with code 1 with an undefined reference to 'vkCreateInstance'

I've been having some trouble setting up a custom Vulkan build and development environment with G++ and VSCode. G++ doesn't seem to be linking to the proper library files, and is not recognizing only one of the Vulkan functions I've defined in main.cpp. That, or it's not properly finding the header files. I followed a video tutorial and did my own tweaking to make it work. (You can find it here https://www.youtube.com/watch?v=tRQ3zqCiKdQ). He creates a .bat file to run the compiler command and sets up some boilerplate tasks.json and launch.json files. The .bat file runs fine and makes it so that I don't have to type out file paths each time. Those work fine. And then I've copied his code as well just to get the build environment working, which I'm stuck on.
I'll admit, I'm not a wizard with command line compilers, but I just can't seem to find any information online that can help me. It's either outdated or the answer doesn't fix my issue. So I thought I'd try here. (first question btw).
I've got a basic, very recent and up-to-date Vulkan installation, a main.cpp file, a build.bat file, and g++ version 11.2.0 (according to MSys2, the latest version). I'm on Windows 64 bit running VSCode. I've included some in order, ending with a capture of my working directory. Afterwards, I'll list what I've tried.
Main.cpp
#include <iostream>
#include <vulkan/vulkan.h>
#include <vulkan/vulkan.hpp>
#include <vulkan/vulkan_core.h>
using namespace std;
int main(){
VkApplicationInfo appInfo = {};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "Pong";
appInfo.pEngineName = "PongEngine";
VkInstanceCreateInfo instanceInfo = {};
instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instanceInfo.pApplicationInfo = &appInfo;
VkInstance instance;
VkResult result = vkCreateInstance(&instanceInfo, NULL, &instance);
if(result == VK_SUCCESS){
cout << "Successful!" << endl;
}
}
Build.bat
#echo off
SET include=-I ./src -I C:/VulkanSDK/1.2.198.1/Include/
SET links= -L /C:/VulkanSDK/1.2.198.1/Lib/vulkan-1.lib
#echo "Building main..."
g++ -Wall %include% %links% -o main src/main.cpp
Directory
Root
|-----src
|-main.cpp
|-build.bat
I have tried almost everything. This is always the output:
"Building main..."
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\brado\AppData\Local\Temp\ccj9NixH.o:main.cpp:(.text+0xabe): undefined reference to `vkCreateInstance'
collect2.exe: error: ld returned 1 exit status
The terminal process "E:\coding\vulkan\personal_vulkan_expirimenting\build.bat" terminated with exit code: 1.
Despite -L supposedly specifying directories to search in, anytime I don't include vulkan-1.lib on the -L tag, it breaks.
I've tried using -l to specify the specific library and that doesn't seem to work
I feel like I've tried every flag under the sun. Someone somewhere said it might have to do with -m64 flag but that wasn't it.
I've added in two seemingly redundant header files that I've manually searched for vkCreateInstance in and included them in the hopes that that would trigger it but in vain
I've edited the file paths every which way to see if the formatting was the issue
I have read and checked the g++ and Vulkan documentation to see if anything could help and I could find seemingly nothing that would have helped. Perhaps I'm looking in the wrong place. But I've been googling for hours.
Before this whole fiasco started, I was also trying to use GLFW and GLM as per the basic Vulkan tutorial that's provided, and that was also having linking problems, which to me, narrows it down to how I'm using the compiler.
What's so confusing is that the intellisense on VS Code recognizes the function, and the linker has no issue with the other functions. It may be a limitation in my understanding of compilers, or I've tunnel-visioned and am not seeing outside the problem, I don't know. I'll admit I'm not insanely familiar with g++, but as I tried to get more familiar with it over the course of this problem (that I've been on for the last couple hours) I couldn't find any documentation Let me know what other information I can provide, maybe there's something I'm leaving out. Thanks so much!

Undefined reference to linker error when using namespaces in headers in c++ [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
I've read through all similar "Undefined reference to" threads I could find but couldn't find a solution. Most of the other threads also involved classes which I don't plan to use for this. The program compiles executes normally if I define the function within the header instead of using an external .cc file. I feel like I'm missing something simple here.
This is the simplest test I could put together that recreates the issue I'm having.
Compiler: g++ (Debian 8.3.0-6) 8.3.0
hntest.h
namespace hntest
{
void pewpew();
}
hntest.cc
#include <iostream>
#include "hntest.h"
namespace hntest
{
void pewpew()
{
std::cout << "pew pew pew!!!" << std::endl;
}
}
hntestmain.cc
#include "hntest.h"
int main(int argc, char* argv[])
{
hntest::pewpew();
}
I'm attempting to compile with:
g++ -lstdc++ hntestmain.cc -o hntestmain
And I get the following linker error:
hntestmain.cc:(.text+0x10): undefined reference to `hntest::pewpew()'
collect2: error: ld returned 1 exit status
I have tried reading through the code of a couple popular C++ libraries as well as some of my own older C (not ++) code and makefiles but haven't been able to find my error. I'm admittedly both and amateur an a bit rusty.
What am I missing?
You are not actually compiling the cpp file that has the definition of pewpew.
Try:
g++ -lstdc++ hntestmain.cc hntest.cc -o hntestmain
The compiler needs to know about all the source files. The header file is dealt with during pre-process and knows to look in the same folder. You can imagine that if you have more files, say 10, 100 or 10000, this would become impossible to manage by using command line. That is why people created build systems like make, cmake and bazel.
For much greater detail see the answer here which is specific for your case of linker error.

Trying to run separated classes in vs code in c++ [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 months ago.
I am new to vs code, after previously using atom a little bit. I am working on c++ with a program that has some classes in separated files. I have already installed the c/c++ extension, but I am facing a problem. When I try to run the program with all it's files included, it shows me the following error:
main.cpp:(.text+0x1f): undefined reference to `Car::sayHello()'
collect2: error: ld returned 1 exit status.
the code is pretty basic.
#include <iostream>
#include "Car.h"
using namespace std;
int main(){
Car c;
c.sayHello();
cout << "Hello world";
return 0;
}
From what I remember from atom, selecting all the files and running them would solve the problem, which I think is the same as running the following command in the terminal:
g++ Car.cpp Car.h main.cpp -o Classes
I have tried these in vs code and the problem seems to appear again. Help is much appreciated.
So if somebody else might have the same problem as me in the future. The problem was that I had declared the constructor and the destructor in the header file, but I hadn't defined them in the .cpp file, after doing that the program seems to be running well. Also as Fred mentioned Car.h is redundant to be mentioned in compilation process, but anyways the program will compile successfully, even if it is used.
So I had the same problem but I figured it out thanks to this link. Follow the instructions and test it out and it should work. There are also other ways to solve this problem based on your preference that are shown on this website. I did notice, however, that once you add multiple folders to your workspace, compiling whatever main.exe file you want becomes difficult since you use the arg ${workspaceFolder}\\*.cpp to compile all the .cpp files in your workspace folder. One way to solve that is to create multiple workspaces, but if anyone knows a more efficient way to do this, please let me know.
you can install fleeox in VS code that will help

Trouble with running code in Visual Studio Code

I know this has been asked many many times, but I am a complete beginner at Linux and I have never used it before this. I am trying to set up VSCode for the past hour or so, and now that I finally got the extensions working (did i?) I tried testing it out and it doesn't work. When I type couple basic lines of code:
#include <iostream>
using namespace std;
int main()
{
cout << "It's not working" << endl;
return 0;
}
And when I try to run the code I get this:
[Running] cd "/home/user/School/CPP Codes/" && g++ test.cpp -o test && "/home/user/School/CPP Codes/"test
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function _start':
(.text+0x20): undefined reference tomain'
collect2: error: ld returned 1 exit status
[Done] exited with code=1 in 0.096 seconds
I read on some of the posts that I should add -o ?! But again I have 0 clue what that means or what I should do exactly. So any help would be insanely appreciated!
Linker errors are indeed hard to debug. After some years you will get them a bit better. Basically the linker tells you, that he finds the file, but does not find your main. As i mentioned in my comment this could be
you did not safe the file
your file got buffer in any way
In my experience, vs code shows the status of saving a bit hidden. where the cross for closing the file is, there is a round dot.

C++ Can't compile simple class header (linker command failed with exit code 1)

the problem seems to be with the compiler I'm using though I'm fairly new to programming so I'm not sure how to mess with that(I'm using VSCode on Mac OSX)
This is my header:
#ifndef STICKMAN_H
#define STICKMAN_H
class Stickman{
public:
Stickman();
};
#endif
This is my source file:
#include "stickman.h"
#include <iostream>
using namespace std;
Stickman::Stickman(){
cout << "Hello\n";
}
This is my main:
#include "stickman.h"
#include <iostream>
int main(){
Stickman figure;
}
This is the ERROR message in the terminal:
Alexandres-MBP:Game alexandrecarqueja$ cd
"/Users/alexandrecarqueja/Desktop/Game/" && g++ main.cpp -o main && "/Users/alexandrecarqueja/Desktop/Game/"main
Undefined symbols for architecture x86_64:
"Stickman::Stickman()", referenced from:
_main in main-d38641.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
You need to call this instead:
g++ main.cpp stickman.cpp -o main
which will also compile stickman.cpp. Then the linker will know what to do. Right now you have a #include stickman.h in your main which declares the class, but does not define it.
The linker sees a constructor is declared (in stickman.h), but does not see how it is implemented (stickman.cpp was not compiled). Hence it is not able to link with the constructor body.
It must be compiler specific because I ran the code in Visual Studio and it built successfully. I would suggest you get the free express/community Visual Studio 2017 IDE software if you happen to have a Windows computer. The code looks fine so I'm personally unsure of what may be causing your issue if it's not compiler related. If you only have a Mac computer, then I suggest maybe looking into other free compilers.
You also receive this error in vscode if your project has a path that includes spaces. As mentioned above you also need to compile all your cpp-files.
To do this in vscode in e.g. macOS Catalina please see my answer here https://stackoverflow.com/a/61331301/1071899
Basically you need to make a tasks.json file with the compiler specific flags. Here you need to include that all *.cpp files should be compiled AND you need to escape the whitespaces by adding "\"${workspaceFolder}\"/*.cpp", instead of "${file}",. Take note of the two \". This will make sure that your project path is surrounded by "" and it will not complain about linker errors.