gcc can't find reference to function DoIt() when linking [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
I am currently becoming crazy. It seems like there is a problem with gcc and it can't open include files can't find the function DoIt() when linking. I tried compiling this code in code blocks and it didn't work so I tried it with G++ in the console and it still didn't work. So I think it's a problem with gcc.
This is my code
main.cpp
#include <iostream>
#include "source.h"
int main()
{
std::cout<<"That works"<<std::endl;
DoIt();
while(true)
{
}
return 0;
}
source.cpp
#include "source.h"
#include <iostream>
void DoIt()
{
std::cout<<"That works too"<<std::endl; //Currently doesn't work
}
source.h
void DoIt();
And this is what I wrote in the terminal
g++ main.cpp -std=c++11 -o result
This is the error message when i run it
/tmp/ccG6X4Bw.o: In function `main':
main.cpp:(.text+0x2d): undefined reference to `DoIt()'
collect2: error: ld returned 1 exit status
I have no clue why it doesn't work

By default, gcc will try to compile and link. Linking without the code from source.cpp will cause the linker to be unable to link the call of DoIt to its code. If you just wish to compile, pass -c to gcc.
From the man page:
When you invoke GCC, it normally does preprocessing, compilation, assembly and linking.
...
-c Compile or assemble the source files, but do not link.
The linking stage simply is not done.

Related

Visual Studio Code can't find reference to function [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 last month.
I'm trying to use Visual Studio for a specific project but I can't get files to link properly.
When including a header file with a defined function in another cpp file im getting an error undefined reference to testFunc() collect2.exe: error: ld returned 1 exit status
Thing is, this exact same code works perfectly in Eclipse. Can someone tell me what I'm doing wrong?
Test.cpp
#include "Other.h"
int main(){
testFunc();
return 0;
}
Other.h
#pragma once
void testFunc();
Other.cpp
#include "Other.h"
#include <iostream>
using namespace std;
void testFunc(){
cout << "HelloWorld";
}
When Buildung, this occours:
Starting build...
C:\MinGW\bin\g++.exe -fdiagnostics-color=always -g C:\Users\johan\cu-workspace\TEst\Test.cpp -o C:\Users\johan\cu-workspace\TEst\Test.exe
C:\Users\johan\AppData\Local\Temp\cck3aAZo.o: In function `main':
C:/Users/johan/cu-workspace/TEst/Test.cpp:5: undefined reference to `testFunc()'
collect2.exe: error: ld returned 1 exit status
Build finished with error(s).
If you build Other.h and Other.cpp as a project, then you need to configure the linker to add Other.lib into test project.
For a simple scenario, you can have all 3 files in one project and they should build just fine.
According to your build info:
C:\MinGW\bin\g++.exe -fdiagnostics-color=always -g C:\Users\johan\cu-workspace\TEst\Test.cpp -o C:\Users\johan\cu-workspace\TEst\Test.exe
You can see that Other.cpp is not in your project, so you might need to add it into your project.
Since you are using VS code, you can write a simple command in terminal to build your code:
C:\MinGW\bin\g++.exe -g C:\Users\johan\cu-workspace\TEst\Test.cpp C:\Users\johan\cu-workspace\TEst\Other.cpp -o C:\Users\johan\cu-workspace\TEst\Test.exe

Cuda compile error when "cuMemGetAddressRange" exists [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.
Here comes the sample codes:
#include <cuda.h>
#include <cuda_runtime.h>
#include <stdio.h>
int main() {
unsigned char* cu_test;
cudaMalloc((void**)&cu_test, 3200);
CUdeviceptr pbase;
size_t psize;
CUresult res = cuMemGetAddressRange(&pbase, &psize, (CUdeviceptr)cu_test);
printf("cu_img_yuv size: %ld", psize);
return 0;
}
While it throws error when compiling whatever the cuda version is(tested from 11.3 to 11.5):
$ nvcc main.cu -o main
/tmp/tmpxft_0000e288_00000000-11_main.o: In function `main':
tmpxft_0000e288_00000000-6_main.cudafe1.cpp:(.text+0x54): undefined reference to `cuMemGetAddressRange_v2'
collect2: error: ld returned 1 exit status
Can someone help pointing out what the problem is plz?
The message:
error: ld returned 1 exit status
denotes that it is a linking error ( see ld ).
To see this is the case, you can run
nvcc -c main.cu -o main.o
and you will not get any error. This is the source-code compilation step!
Solution:
You need to explicitly specify linkage with the CUDA driver stub library:
nvcc main.cu -o main -lcuda
That is because cuMemGetAddressRange() is part of the CUDA Driver API, not the CUDA runtime API.
NOTE: you did not cudaFree() the allocated memory, you might want to fix this!
Edit: (credit to #talonmies comment) You do not have to explicitly link against the CUDA runtime library (-lcudart), because nvcc will automatically link against it.

C/C++ linking issue with a very simple setup [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 5 years ago.
I'm getting an "undefined reference" error when trying to compile and link a very simple app that has a dependency on a third-party library.
My main.c looks like this:
#include "ss7cp.h"
/*
extern "C" {
void EINSS7CpMain_CpInit(void);
}
*/
int main() {
EINSS7CpMain_CpInit();
}
The third-party header file has:
#if defined (__cplusplus) || defined (c_plusplus)
extern "C" {
#endif
...
void EINSS7CpMain_CpInit(void);
The definition of this function is in an archive:
$ nm -g /path/to/lib/libsign_64_uthr.a | grep EINSS7CpMain_CpInit
0000000000005ae0 T EINSS7CpMain_CpInit
U EINSS7CpMain_CpInit
U EINSS7CpMain_CpInit
By the "T" above, the function must be defined in the text/code section of one of the libs in the archive.
I'm currently not using a Makefile, but just trying to quickly build this simple app entirely from the command line:
g++ -I/path/to/include -L/path/to/lib -lsign_64_uthr -D__EXTENSIONS__ -DLINUX main.c
(The documentation told me to define both __EXTENSIONS__ and LINUX). The rest of the above is pretty straightforward. What I'm getting is:
/tmp/ccvgmIJ8.o: In function `main':
main.c:(.text+0x5): undefined reference to `EINSS7CpMain_CpInit'
collect2: error: ld returned 1 exit status
I've tried both as a C file (main.c) and C++ file (main.cpp, enabling the extern "C" block), but no difference. (Can I assume g++ decides on C versus C++ just by the file extension?)
I've even just compiled the file (with -c) and took a look at the contents of the resultant main.o object file and saw the text "EINSS7CpMain_CpInit" as it is, unmangled (or maybe that's just a debug symbol?)
What are some diagnostics steps I can take to see what I'm missing? Do I actually need to create a Makefile and split the compile and link steps?
It's been ages since I last did any C/C++ and even back when I did it, I usually wasn't the one who had to create the Makefiles from scratch, so I'm probably missing some very fundamental stuff here...
My best guess would be parameter ordering, the lib should come after the source file:
g++ -I/path/to/include -D__EXTENSIONS__ -DLINUX main.c -L/path/to/lib -lsign_64_uthr

Compile GLFW code with g++ [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 7 years ago.
I am just starting to get into C++ coding, specifically OpenGL (GLFW at this stage). I am using MinGW, and compiling my code using a batch file:
g++ -time -v -I lib/glfw-3.1.2/include -o "bin/OpenGL Test" src/Main.cpp
My test code is this:
#include <iostream>
#include <GLFW/glfw3.h>
using namespace std;
int main() {
if(!glfwInit()) {
return 1;
}
glfwTerminate();
return 0;
}
To me this seems like it should compile and run correctly, but it throws errors at the two GLFW functions, claiming that they are 'undefined references'.
What have I done wrong?
'undefined references' is a common error. You should compile your cpp first. Then link it with the library to form executable file.

Undefined reference to __dso_handle_ - compiling C++ on cygwin

I have a basic Hello World C++ program that I am trying to compile and run on cygwin with g++. The code is:
#include <iostream>
using namespace std;
int main() {
cout << "Hello world." << endl;
return 0;
}
Compiling it with: g++ helloWorld.cpp -o h results in the following error:
/tmp/ccDO1S4J.o:helloWorld.cpp:(.rdata$.refptr.__dso_handle[.refptr.__dso_handle]+0x0): undefined reference to__dso_handle'
collect2: error: ld returned 1 exit status`
I have been reading up on some other threads that indicate that it might be a linker problem and that invoking the linker separately or with verbose output might lead to some more clues.
So, I did:
1. g++ -c helloWorld.cpp -o helloWorld.o (this works - no errors).
2. ld -o h helloWorld.o causes a lot of undefined reference to __main or std::cout etc. errors.
I think this is a linking issue and that I need to link another library perhaps. Any pointers on how to solve this are most welcome.
Re-installing g++ via the installer application on cygwin worked.