The mac cannot compile c/c++.

Asked 2 years ago, Updated 2 years ago, 35 views

I wrote the following C/C++ code on my mac, but I cannot compile it because of the error below.

intarray.cpp

#include<stddef.h>
# include <stdlib.h>
# include <stdio.h>


US>struct IntArray}
  int*elems;
  size_tnumElems;
};

void IA_init(IntArray* object);
void IA_cleanup (IntArray* object);
void IA_setSize (IntArray* object, size_t value);
size_tIA_getSize (IntArray* object);
void IA_setElem(IntArray* object, size_t index, int value);
intIA_getElem (IntArray* object, size_t index);


voidIA_init(IntArray* object, size_tvalue) {
  object->numElems=0;
  object->elems=0;
}

void IA_cleanup (IntArray* object) {
  free(object->elems);
}

voidIA_setSize(IntArray*object,size_tvalue){
  if(object->elems!=0)free(object->elems);
  object->numElems=value;
  object->elems=(int*)malloc(value*sizeof(int));
}

voidIA_setElem(IntArray* object, size_tvalue, int index) {
  if(index>=object->numElems){
    printf("bad index");
    return;
  }
  object->elems [index] = value;
}

intIA_getElem(IntArray* object, size_t index){
  if(index>=object->numElems){
    printf("bad index");
    return 0;
  }
  return object->elems [index];
}

int main() {
  IntArray powersOf2;
  IA_init(&powerOf2);
  IA_setSize (&powersOf2,8);
  IA_setElem(&powerersOf2,0,1);
  inti;
  for(i=1;i<8;i++){
    IA_setElem(&powerOf2,i,2*IA_getElem(&powerOf2,i-1));
  }
  IA_cleanup (&powerOf2);

  return 0;
}

command

g++-vintarray.cpp-o intarray

error

Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin 15.2.0
Thread model: posix
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.11.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name intarray.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 253.9 -v -dwarf-column-info -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2 -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/username/workspaces/c_plus_plus_workspace/class -ferror-limit 19 -fmessage-length 144 -stack-protector1-mstackrealign-fblocks-fobjc-runtime=macosx-10.11.0-fencode-extended-block-signature-fcxx-exceptions-fmax-type-align=16-fdiagnostics-show-option-fcolor-diagnostics-o/varbz1/falz1/holds59
clang-cc1 version 7.0.2 based upon LLVM 3.7.0 svn default target x86_64 - apple-darwin 15.2.0
ignore nonexistent directory "/usr/include/c++/v1"
# include "..." search starts here:
# include <...> search starts here:
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.11.0 -o intarray /var/folders/2z/x8jb59gs1lz7n1hhbhk_st8c0000gn/T/intarray-f42434.o -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "IA_init(IntArray*)" from:
      _main in intarray-f42434.o
     (maybe you mean:__Z7IA_initP8IntArraym)
ld —symbol(s) not found for architecture x86_64
clang:error:linker command failed with exit code 1 (use-v to see invocation)

When I searched, I saw several reports of the same error, but I tried the solution, but it didn't work.

However, I was able to compile the code below.

hello.cpp

#include<stdio.h>

int main() {
  printf("Hello, world";
  return 0;
}

hello.cpp

#include<iostream>

int main() {
  std::cout<<"Hello, world"<<"\n";
  return 0;
}

Why can't I compile the code above?

Run Environment

Mac El Captan bash

add

By the way, there was an error in the code below.

intarray2.h

#include<stddef.h>
# include <stdio.h>
# include <stdlib.h>

classIntArray {
  public:
    void init();
    void cleanup();
    voidsetSize(size_tvalue);
    size_t getSize();
    voidsetElem(size_t index, int value);
    int getElem(size_t index);

  private:
    int*elems;
    size_tnumElems;
};

intarray2.cpp

#include<stddef.h>
# include <stdio.h>
# include <stdlib.h>

classIntArray {
  voidIntArray::init(){
    This ->numElems=0;
    This ->elems = 0;
  }

  voidIntArray::cleanup(){
    This ->numElems=0;
    free(this->elems);
  }

  voidsetSize(size_t,value){
   if(this->elems!=0)free(this->elems);
   this ->numElems=value;
   This ->elems=(int*)malloc(value*sizeof(int));
  }

  void IntArray::setElem(size_tvalueem, int index){
    if(index>=this->numElems){
      printf("bad index");
      return;
    }
    this ->elems [index] = value;
  }

  int IntArray::getElem(size_t index){
    if(index>=this->numElems){
      printf("bad index");
      return1;
    }
    return this ->elems [index];
  }
}

main.cpp

#include "IntArray2.h"

int main() {
  IntArray powerOf2;
  powerOf2.init();
  powerOf2.setSize(8);
  inti;
  for(i=0;i<8;i++){
    powerOf2.setElem(i,2*powerOf2.getElem(i-1));
  }
  powerOf2.cleanup();
  return 0;
}

error

Undefined symbols for architecture x86_64:
  "IntArray::init()" from:
      _main in main-d429d9.o
  "IntArray::cleanup()" from:
      _main in main-d429d9.o
  "IntArray::getElem(unsigned long)" from:
      _main in main-d429d9.o
  "IntArray::setElem(unsigned long, int)" from:
      _main in main-d429d9.o
  "IntArray::setSize(unsigned long)" from:
      _main in main-d429d9.o
ld —symbol(s) not found for architecture x86_64
clang:error:linker command failed with exit code 1 (use-v to see invocation)

c++

2022-09-30 14:51

3 Answers

in the Code, all references to IntArray (other than the structure definition) must be structure IntArray.This will fix most of the compiler raised warnings.

Then the protocol for IA_init() and the call to IA_init() do not match the actual function signature. The actual function has 2 parameters, which the call and the protocol only have a single parameter.

This function:void IA_setElem(struct IntArray* object, size_t value, int index) {is passing asize_t but the receiving field in the IntArray is of type

this line:IA_setElem(&powerOf2,i,2*IA_getElem(&powerOf2,i-1));is passing the intivalue to two different functions where those functions are expected a size_toto code.

function:IA_init(), setting the int pointer field:elems to 0 will work in this scenario, however, the assignment is expecting a pointer, not an integer, Suggest using:object->elems=NULL;

When calling malloc() inc, do not cast the returned value, its' type is void* which can be assigned to any pointer. Casting just clusters the code, makes understanding, debug, and maintenance much more difficult.The code contains some 'magic' numbers. (like 8) 'magic' numbers make the code much more different to understand, debug, maintain.Suggest using #define statements to give the magic numbers making full names, .then using these means throughout the code.

regarding this line:if(index>=object->numElems){.It causes the compiler to raise a warning about a comparison between signed(int) and unsigned(size_t) values.suggest passing the index/a character>index/a character>


2022-09-30 14:51

Fixes for the First Example

Use the following instead of the C header

#include<cstdio>
# include <cstdlib>

As IA_init has already been pointed out, the prototype and implementation are different
Match the arguments.

void IA_init(IntArray*object){
  object->numElems=0;
  object->elems=0;
}

Fixes in the second example (not enclosed in class definitions already pointed out)

voidsetSize(size_t,value){
, is unnecessary.There is no class qualification.
Change as follows:
voidIntArray::setSize(size_tvalue){

void IntArray::setElem(size_t valueem, int index){
value appears to be a mistake in value.
Change as follows:
void IntArray::setElem(size_t value, int index){

First of all, I think I can compile it now.


2022-09-30 14:51

#include<stddef.h>
# include <stdlib.h>
# include <stdio.h>


structure IntArray
{
  size_t*elems;
  size_tnumElems;
};

# if 0
void IA_init(IntArray* object);
void IA_cleanup (IntArray* object);
void IA_setSize (IntArray* object, size_t value);
size_tIA_getSize (IntArray* object);
void IA_setElem(IntArray* object, size_t index, int value);
intIA_getElem (IntArray* object, size_t index);
#endif

// void IA_init (IntArray* object, size_t value) {
voidIA_init(struct IntArray* object){

  object->numElems=0;
  object->elems = NULL;
}

void IA_cleanup (struct IntArray* object) {
  free(object->elems);
}

voidIA_setSize(struct IntArray* object, size_tvalue){
  if(object->elems!=0)free(object->elems);
  object->numElems=value;

  object->elems=malloc(value*sizeof(size_t));
  if(NULL==object->elems)
  { // then malloc failed
      error("malloc failed";
      exit(EXIT_FAILURE);
  }
  // implied else, malloc successful
}

voidIA_setElem(struct IntArray* object, size_t value, size_t index){
  if(index>=object->numElems){
    printf("bad index");
    return;
  }
  object->elems [index] = value;
}

size_tIA_getElem(struct IntArray* object, size_t index){
  if(index>=object->numElems){
    printf("bad index");
    return 0;
  }
  return object->elems [index];
}

int main(void){
  structure IntArray powersOf2;
  IA_init(&powerOf2);
  IA_setSize (&powersOf2,8);
  IA_setElem(&powerersOf2,0,1);
  size_ti;

  for(i=1;i<8;i++){
    IA_setElem(&powerOf2,i,2*IA_getElem(&powerOf2,i-1));
  }
  IA_cleanup (&powerOf2);

  return 0;
}


2022-09-30 14:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.