简体   繁体   中英

ld: symbol(s) not found for architecture x86_64 - Error

I am trying to run a code on VSCode on Mac OS Catalina 10.15.2.

I am getting this Error on this function.

 $ g++ main.cpp
Undefined symbols for architecture x86_64:
 "nalea(int)", referenced from:_main in main-508a59.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 the main file I am using -

#include<iostream>
#include "proba.h"
#include "constantes.h"
#include "affichage.h"

int main(){
int a = nalea(60);
std::cout<<a;
//int a = InitAffichage();
return 0;
}

This is the proba.cpp file from where the function is called.

 #include <cstdlib>     // pour rand()
 #include <cstdio>      // pour fprintf()
 #include <cmath>       // pour floor()
 #include "proba.h"     // types et déclaration des fonctions


   int nalea( int max)
  {

       return (int)floor(rand()*((float)max)/RAND_MAX );
   }

This is proba.h header file

     int nalea(int max);

Please help me, I am new to C++....

I have encountered this multiple times, and in most cases it is related to spaces in you path.

The simple test you can make is to put your project in a folder without spaces in its path eg your user home directory.

To fix it so you can have the project in any folder, you need to first make a tasks.json file with the build information and compile all *.cpp according to the guide from vscode https://code.visualstudio.com/docs/cpp/config-clang-mac

  1. Open your main.cpp
  2. Go to the menu "Terminal", select "Configure Default Build Task"
  3. Select "C/C++: clang++ build active file". You should get a tasks.json file with the following information

    { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: clang++ build active file", "command": "/usr/bin/clang++", "args": [ "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "options": { "cwd": "/usr/bin" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true } } ] }
  4. Now the critical part is that you should change "${file}", to "\"${workspaceFolder}\"/*.cpp", . Take note of the two \" , which will make sure to handle any spaces in your path, this is not mention in the vscode guide linked earlier

  5. To build go to the menu "Terminal" and select "Run Build Task" or press "Shift+Cmd+B"

This is likely a common "rookie mistake" when starting out with C++. Traditionally, the C++ compiler will compile the code in a single .cpp file into what is called an object file.

The object file contains the native code for the compiler target architecture, but not in a format that can be run directly. To transform the object file into an executable, a separate linking stage is needed. The linker will turn a set of object files into a single executable binary that can be run.

Traditionally, the compilation process for a single file would be someting like

g++ -c main.cpp
ld main.o -lstdc++

The first command produces the object file main.o that is used by the second invocation to the linker (ld) which produces the executable file (in this case using the default name a.out ). To link multiple files into the same binary, one could use

g++ -c main.cpp
g++ -c proba.cpp
ld main.o proba.o -lstdc++

The g++ command can not only invoke the compiler, but also the linker using a shorthand notation for the above. So your command

g++ main.cpp

tells the compiler to compile main.cpp into a temporary object file, then link it to the executable a.out .

If you were to run

g++ main.cpp proba.cpp

it would compile both files and link them into a single binary, which would solve your problem.

Best of luck with your learning

You simply need to compile all your *.cpp files. Your compile command should look somthing like this:
g++ proba.cpp main.cpp [any other cpp files I don't know about]

I don't think you can refer to another * .cpp file in C ++ . you can use proba.cpp as a .h and then save it as a library !. I'm sorry if I made a mistake in English

The problem occurs, when your files are not linked to each other or, better said, to the project.

In addition to the answer of @sweenish for terminal usage. I am using clion and had to use/add the following syntax/project files in CMakeLists.txt

add_executable(<<projectname>> main.cpp first.h first.cpp secound.h secound.cpp ...)

I had the same issue when i was compiling Erlang.

This fixed the issue for me: https://developer.apple.com/forums/thread/694062

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM