Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Ways to resolve the error - clang: error: linker command failed with exit code 1
Next
Answered
Juliette Faure
  • 8
Juliette Faure
Asked: May 18, 20222022-05-18T10:42:24+00:00 2022-05-18T10:42:24+00:00In: c

Ways to resolve the error – clang: error: linker command failed with exit code 1

  • 8

. Advertisement .

..3..

. Advertisement .

..4..

Hi developer experts, I have a small but frustrating use case, and so far, I couldn’t get my head around this problem & ideal solution. I am running my command and facing one problem with the clang: error: linker command failed with exit code 1. Below is the command I used:

#include <stdio.h>
 
 int power(int m, int n);
 
 int main()
 {
 int i;
 
 for (i=0; 1<10; ++i)
  printf("%d %d %d\n", i, power(2,i), power(-3,i));
 
 return 0;
 }

When I run it, I get the following error:

Undefined symbols for architecture x86_64:
  "_power", referenced from:
  _main in main.o
 ld: symbol(s) not found for architecture x86_64
 clang: error: linker command failed with exit code 1 (use -v to see invocation)

I am looking forward to gaining some knowledge from all experts. Thank you, guys!

linker command failed with exit code 1
  • 2 2 Answers
  • 223 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-07T09:11:15+00:00Added an answer on June 7, 2022 at 9:11 am

    The cause:

    Why do I get clang: error: linker command failed with exit code 1?

    There is no definition in code in the function which you has just declared. Compiler(here clang), cannot link power function with its definition at the time of linking so linker throws an error in this type of situation and the error happens.

    Solution:

    If you define

    int power(int x, int y) 
     { 
     \*do calculation*/
     }

    Your power function declaration will be linked to its definition by the linker. You won’t get an error. I have created function for integer number as below:

    #include <stdio.h>
    int power(int base, int exp);
    int main()
    {
    int i;
    
    for (i=0; i<10; ++i)
     printf("%d %d %d\n", i, power(2,i), power(-3,i));
    
    return 0;
    }
    
    int power(int base, int exp)
    {
     int result = 1;
     while (exp)
     {
     if (exp & 1)
     result *= base;
     exp >>= 1;
     base *= base;
     }
    
     return result;
    }

    This gcc file.c compilation is required.

    • 8
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Liam Rocher
    2022-05-25T20:16:52+00:00Added an answer on May 25, 2022 at 8:16 pm

    The definition of function int power (int base,int n) was not included in your book’s main pages.

    You must define the function that you are declaring prototype of.

    The following definition will ensure that your code compiles exactly as you want it to.

    int power (int base,int n){
    int i,p;
    p=1;
    for (i=1;i<=n;++i)
    p=p*base;
    return p;
    }

    PRE-EDIT NOW THIS ISN’T RELEVANT, BUT USEFULL

    You may want to use the math.h function pow(), I believe.

    double pow(double a, double b)

    C library function pow(double a, double b) returns a, which is twice as powerful as b. This function returns double values so that "%lf" will print the correct specifier.

    You will only need to include the header file in this instance

    #include<math.h>

    Include it in your program.

    It is not necessary to provide function declaration int power(int m, int n);

    Your error is caused by I being given as the parameter to pow(). This is because pow() will compile your source code after you have included math.h and used pow() to replace i with any other integer numbers.

    printf("%lf %lf %lf\n", i, pow(2,3), pow(3,2));

    You will get a good result if you combine it with

    for (i=0; i<10; ++i){
    printf("%lf %lf %lf\n", i, pow(2,i), pow(-3,i));
    }

    It throws the same error, so pow() should only accept constants input.

    You can also declare if math.h is not required.

    extern double pow (double base, double exponent); 

    This will allow you to link with the library code directly without the use of the math.h include files. Here is an example.

    int main() {
    extern double pow (double base, double exponent);
    printf("%lf",pow( 8.0, 8.0 ));
    return 0;
    } 

    You can find more information on pow() on the man page for Linux, i.e. man pow.

    • 10
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.