Undefined symbols for architecture x86_64: missing package? - c++

I am doing different exercises in c++ for preparing my exam at university.
I am pretty sure they are all without heavy mistake and should complement.
All codes can't complement with the same error log:
Undefined symbols for architecture x86_64:
[hundreds lines of error log]
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The error log between is about every single line of code.
I am wondering if I've missed to install some package for complement c++ on my device.
Code Example:
#include <iostream>
#include <new>
int main () {
int n;
std::cout<<"How many value do you want to enter to your list?"<< std::endl;
std::cin>>n;
int* numbArray = new int[n];
for(int i = 0; i<n; i++) {
std::cout<<"Enter the"<< i+1 <<". value!"<<std::endl;
std::cin>>numbArray[i];
}
std::cout << "List of value: " << std::endl;
for(int i=0; i<n; i++ ) {
std::cout<<numbArray[i]<<" "<<std::endl;
}
std::cout<<"end of arrays"<<std::endl;
delete[]numbArray;
return 0;
}
My operating System is macOS Catalina 10.15.2
Thanks for your help.

There are no explicit/implicit linker symbols. This is a symbol in the C++ standard library. How to you call the compiler and linker? Do you happen to link with "gcc" instead of "g++", as it should be?
Just as Erlkoeing said, using "g++" instead of "gcc" for calling the compiler and linker works fine.

Related

Following c++ code works in leetcode but not in my vscode, why? [duplicate]

This question already has an answer here:
G++ Compiler warning when using c++ 17 updates
(1 answer)
Closed 1 year ago.
I am currently working on a leetcode question, and try to track down the code process in my end, this is my solution:
#include <iostream>
#include <vector>
#include <stack>
#include <utility>
using namespace std;
vector<int> direction{-1, 0, 1, 0, -1};
int maxAreaOfIsland(vector<vector<int>>&grid){
int m = grid.size(), n = m ? grid[0].size() : 0, local_area, area = 0, x, y;
for (int i = 0; i < m; ++i){
for (int j = 0; j < n; ++j){
if(grid[i][j]){
local_area = 1;
grid[i][j] = 0;
stack<pair<int, int>> island;
island.push({i, j});
while(!island.empty()){
auto [r, c] = island.top(); \\problem line, vscode can't understand it
island.pop();
for (int k = 0; k < 4; ++k){
x = r + direction[k], y = c + direction[k + 1];
if(x>=0 && x<m && y>=0 && y<n && grid[x][y]==1){
grid[x][y] = 0;
++local_area;
island.push({x, y});
}
}
}
area = max(area, local_area);
}
}
}
return area;
}
this code works on the leetcode side, but not mine, here is the warning
[Running] cd "c:\Users\chen1\OneDrive\Desktop\C_C++tut\" && g++ leetcode695.cpp -o leetcode695 && "c:\Users\chen1\OneDrive\Desktop\C_C++tut\"leetcode695
leetcode695.cpp: In function 'int maxAreaOfIsland(std::vector<std::vector<int> >&)':
leetcode695.cpp:23:16: warning: structured bindings only available with -std=c++17 or -std=gnu++17
auto [r, c] = island.top();
^
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
[Done] exited with code=1 in 0.932 seconds
Can someone explains why, although I get an alternative way to replace it, it is still annoying and perplexing
thanks for helping
additionally!!!
I actually have my main function; the problem here is a syntax error where leetcode's compiler recognizes it, but not g++, the line that causes the problem is auto [r, c] = island.top();, if I alter it to
int r = get<0>(island.top());
int c = get<1>(island.top());
then it works fine, I just don't understand why leetcode compiler can understand it, but not g++
The linker error (not the warning) is what is causing the build to fail (scroll sideways!):
[...]crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
occurs in MinGW gcc when your code lacks either a main() or WinMain() entry point. As yours does. I guess leetcode (which I have never heard of or used) provides a test harness for you to run the function? It compiles - the message is a linker error, so it cannot form an executable - you need a main().
With respect to the warning, again you need to scroll to the end of the message:
leetcode695.cpp:23:16: warning: structured bindings only available
with -std=c++17 or -std=gnu++17
auto [r, c] = island.top();
can be resolved by specifying C++17 (or higher) compilation (or not using structured bindings). A Windows/VSCode specific solution is discussed at G++ Compiler warning when using c++ 17 updates, but fundamentally it is about setting the compiler switch -std=c++17.

Problem compiling and linking voxelyze library

I'm rather new in c++, and I've just installed Ubuntu in my windows PC a few weeks ago.
I have to work with a static library called Voxelyze (I downloaded it from Here. There you can see where are the includes, libs and makefile). And I'm trying to compile an example code (I named it testeo.cpp).
My testeo.cpp path is: c/Users/Familia/Desktop/MEMORIA/F/galib247/galib247/examples
And the voxelyze library path is: c/Users/Familia/Desktop/MEMORIA/F/galib247/galib247/examples/Voxelyze-master
This is the code:
#include "Voxelyze.h"
#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
cout << "Testing voxelyze library\n";
CVoxelyze Vx(0.005); //5mm voxels
CVX_Material* pMaterial = Vx.addMaterial(1000000, 1000); //A material with stiffness E=1MPa and density 1000Kg/m^3
CVX_Voxel* Voxel1 = Vx.setVoxel(pMaterial, 0, 0, 0); //Voxel at index x=0, y=0, z=0
CVX_Voxel* Voxel2 = Vx.setVoxel(pMaterial, 1, 0, 0);
CVX_Voxel* Voxel3 = Vx.setVoxel(pMaterial, 2, 0, 0); //Beam extends in the +X direction
Voxel1->external()->setFixedAll(); //Fixes all 6 degrees of freedom with an external condition on Voxel 1
Voxel3->external()->setForce(0, 0, -1); //pulls Voxel 3 downward with 1 Newton of force.
for (int i=0; i<100; i++){
Vx.doTimeStep(); //simulate 100 timesteps.
cout << "Current position: " << (float)Voxel3->position().z << "\n";
}
return 0;
}
This code simply puts three voxels in a row, excerts a downward force in the third one, and then simulates its behavior, and for each timestep simulation it shows its displacement.
Ok, now, in my ubuntu command (after doing sudo apt-get -y update, sudo apt-get -y upgrade and sudo apt-get install build-essential) I firstly go to the voxelyze directory and I do "make clean" and then "make", so that it makes everything that needs to be made:
$ cd /mnt/c/Users/Familia/Desktop/MEMORIA/F/galib247/galib247/examples/Voxelyze-master
$ make clean
$ make
So far so good.
Then, I return to my testeo.cpp path by simply doing cd .. and, using GCC, I try to compile testeo.cpp this way:
$ cd..
$ g++ -Wall -o testeo testeo.cpp -I./Voxelyze-master/include -L./Voxelyze-master/lib/ -lvoxelyze.0.9
But I get the following error message:
testeo.cpp: In function ‘int main()’:
testeo.cpp:15:14: warning: unused variable ‘Voxel2’ [-Wunused-variable]
15 | CVX_Voxel* Voxel2 = Vx.setVoxel(pMaterial, 1, 0, 0);
| ^~~~~~
/usr/bin/ld: ./Voxelyze-master/lib//libvoxelyze.0.9.a(VX_LinearSolver.o): in function `CVX_LinearSolver::CVX_LinearSolver(CVoxelyze*)':
VX_LinearSolver.cpp:(.text+0x123): undefined reference to `pardisoinit'
/usr/bin/ld: ./Voxelyze-master/lib//libvoxelyze.0.9.a(VX_LinearSolver.o): in function `CVX_LinearSolver::solve()':
VX_LinearSolver.cpp:(.text+0x2a13): undefined reference to `pardiso'
/usr/bin/ld: VX_LinearSolver.cpp:(.text+0x2b17): undefined reference to `pardiso'
/usr/bin/ld: VX_LinearSolver.cpp:(.text+0x2c26): undefined reference to `pardiso'
/usr/bin/ld: VX_LinearSolver.cpp:(.text+0x2e30): undefined reference to `pardiso'
collect2: error: ld returned 1 exit status
Well I don't care much about the warning message, but about the "undefined reference to error".
I think it's an internal library error, (or maybe I'm doing something wrong in my linkage, IDK) because it's complaining about some "LinearSolver" class functions and a "pardiso" thing... but I'm not using them in my testeo.cpp code (at least not directly).
Well, I don't know how to make this thing work, and my engineering degree is on the line here hahaha.
I hope you can help me here, I'd thank you a lot. I'm rather new in this C++-Linux world, but whatever additional information you need in order to solve this mystery, I'll try to do my best to help.
Thanks.

Undefined reference to timeGetTime() in 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 5 years ago.
I'm using g++ command line under Windows 10 to build a basic timing loop and am getting the error: " undefined reference to `timeGetTime#0' " when attempting to compile.
The code, itself, is pretty simple:
#include <windows.h>
#include <iostream>
using namespace std;
int main(){
int start = timeGetTime();
int finish = 10000;
int benchmarks [9] = {1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000};
int i = 0;
int time = timeGetTime() - start;
while( time < finish){
if(time > benchmarks[i]){
cout << benchmarks[i] / 1000 << endl;
i++;
}
return 0;
}
Not sure what I need to do to get g++ to play nicely with the WinAPI. I can't help but wonder if it's an issue with the linker.
You have to link libwinmm.
Undefined reference is an error which occures when your compiler knows that the function exist, it knows its prototype, but can't find its corpse. When it is not one of your functions, it surely means you miss to link a library.

Compiling C++ program using H5Cpp.h header file

I'm need to create a HDF5 file. I'm using the H5Cpp.h header file.
I'm trying to compile the C++ code below on OSX 10.11 El Capitan.
#include "include/hdf5-1.10.0-patch1/c++/src/H5Cpp.h"
using namespace H5;
const int NX = 5;
const int NY = 5;
const H5std_string FILE_NAME( "SDS.h5" );
const H5std_string DATASET_NAME( "IntArray" );
int main(){
int i, j;
int data[NX][NY]; // buffer for data to write
for (j = 0; j < NX; j++){
for (i = 0; i < NY; i++)
data[j][i] = i + j;
}
H5File file(FILE_NAME, H5F_ACC_TRUNC);
hsize_t dimsf[2] = {NX, NY};
DataSpace dataspace(2, dimsf);
DataSet dataset = file.createDataSet(DATASET_NAME, PredType::NATIVE_INT,
dataspace);
// Attempt to write data to HDF5 file
dataset.write(data, PredType::NATIVE_DOUBLE);
return 0;
}
I keep getting this error,
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [all] Error 1
Here's the verbose output - https://gist.github.com/gkarthik/e21d2f83baffc2d2eb1b883696c44df8
Thanks!
A few tips:
I think that you have to compile the HDF5 library yourself if you haven't the pre-built binaries. At the HDF5 website there are no pre-built binaries for Mac (as far as I can see).
In windows, to compile a C++ application to use the HDF5 library you have to indicate to the linker the following dependencies (in this order), maybe in mac is also imperative:
szip.lib zlib.lib hdf5.lib hdf5_cpp.lib
If you download some pre-built binaries, there is a file that will indicate the steps to compile your C++ program. In windows it is called: "USING_HDF5_CMake.txt" and "USING_HDF5_VS.txt". You can find these files for example here.

C++:Linker command with exit code -1 in xcode

I was implementing a suffix array in xcode using c++ when I got the following error:
ld: 32-bit RIP relative reference out of range (100000121018926 max is +/-4GB): from _main (0x100001310) to _L (0x5AF417B0F130) in '_main' from /Users/priya/Library/Developer/Xcode/DerivedData/cfquestions-boqlvazrozappdeetfhesfsohczs/Build/Intermediates/cfquestions.build/Debug/cfquestions.build/Objects-normal/x86_64/main.o for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
My code for the suffix array is as follows:
char a[size1];
int ans[size2][size2];
struct node{
int gg[2],pos;
}L[size1];
int step=1,ct=1;
bool comp(node a, node b){
return a.gg[0]==b.gg[0]?(a.gg[1]<b.gg[1]?1:0):(a.gg[0]<b.gg[0]?1:0);
}
int main(){
int TT;
cin>>TT;
while(TT--){
set<int> s;
//a.clear();
int n=strlen(a);
scanf("%s",a);
for(int i=0;i<strlen(a);i++)ans[0][i]=a[i]-'a';
for(;ct<strlen(a);step++,ct<<=1){
for(int i=0;i<n;i++){
L[i].gg[0]=ans[step-1][i];
L[i].gg[1]=i+ct<n?ans[step-1][i+ct]:-1;
L[i].pos=i;
}
sort(L,L+n,comp);
for(int i=0;i<n;i++)
ans[step][L[i].pos]=i>0&&L[i].gg[0]==L[i-1].gg[0]&&L[i].gg[1]==L[i-1].gg[1]?ans[step][L[i-1].pos]:i;
}
for(int i=0;i<n;i++)
{
if(s.find(ans[step-1][i])!=s.end()){
}
else s.insert(ans[step-1][i]);
}
cout<<s.size()<<endl;
}
return 0;
}
P.S: The code runs fine on any other code. I tried even much more complex codes; but it works fine. Hence there must be something wrong with this piece of code- but I am not able to figure out what!
Any help would be appreciated thanks!!
edit: The reason is the size of ans, as pointed out in the comment by #WhozCraig after you added the sizes. My guess regarding why the linker error message names _L is that the compiler put main at a lower address and then the global data in order, giving the too big RIP offset to L.
In one of the for loops you have i = 0 and access L[i-1], which is a very large number for the array index .
edit: but that should give a runtime failure or error and not give a linker error.