www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - linking trouble

reply hridyansh thakur <hridyanshthakur gmail.com> writes:
i am on windows i have tried
DMD
LDC

and i am getting same linking error with linking my c++ object

i am doing by the official tutorial (dlang spec book)

here is my app.d code

import std.stdio;

void main()
{
	//writeln("Edit source/app.d to start your project.");
	int[] m = someFUN(44,55);
	ulong k = m.length;

	for (int i=0;i<k;i++)
	writeln(m[i]);


}

auto someFUN(int j , int k){
	int[] some = new int[j];

	for (int i =0 ; i<j ; i++)
		some[i] =  i;

		return some ;
}

extern(C++){int*file_io}


here is the C++ code

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

FILE *fp;

int*file_io(){

char name[20] ;

std::cout << "please enter the file name " << '\n';
std::cin >> name;

   fp = fopen(name,"r+");
  char  a  = 'a';
   int  n  =  0 ;
   while (!feof(fp)) {
     a = fgetc(fp);
     if (a=='\n') {
       n++;
     }
   }

  int *p  = (int*)calloc(n,sizeof(int));
  for (size_t i = 0; i < n ; i++) {
    fscanf(fp,"%d",(p+i));
  }

return p;

}
Sep 06 2018
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 07/09/2018 4:03 AM, hridyansh thakur wrote:
 i am on windows i have tried
 DMD
 LDC
 
 and i am getting same linking error with linking my c++ object
 
 i am doing by the official tutorial (dlang spec book)
 
 here is my app.d code
 
 import std.stdio;
 
 void main()
 {
      //writeln("Edit source/app.d to start your project.");
      int[] m = someFUN(44,55);
      ulong k = m.length;
 
      for (int i=0;i<k;i++)
      writeln(m[i]);
 
 
 }
 
 auto someFUN(int j , int k){
      int[] some = new int[j];
 
      for (int i =0 ; i<j ; i++)
          some[i] =  i;
 
          return some ;
 }
 
 extern(C++){int*file_io}
That definition isn't complete. Missing at the very least ``();`` to make it a function declaration.
 here is the C++ code
 
 #include <iostream>
 #include <stdio.h>
 #include <stdlib.h>
 
 FILE *fp;
 
 int*file_io(){
 
 char name[20] ;
 
 std::cout << "please enter the file name " << '\n';
 std::cin >> name;
 
    fp = fopen(name,"r+");
   char  a  = 'a';
    int  n  =  0 ;
    while (!feof(fp)) {
      a = fgetc(fp);
      if (a=='\n') {
        n++;
      }
    }
 
   int *p  = (int*)calloc(n,sizeof(int));
   for (size_t i = 0; i < n ; i++) {
     fscanf(fp,"%d",(p+i));
   }
 
 return p;
 
 }
So what is the errors you're getting? And what are the commands you're executing?
Sep 06 2018
parent reply hridyansh thakur <hridyanshthakur gmail.com> writes:
On Thursday, 6 September 2018 at 16:59:43 UTC, rikki cattermole 
wrote:
 On 07/09/2018 4:03 AM, hridyansh thakur wrote:
 [...]
That definition isn't complete. Missing at the very least ``();`` to make it a function declaration.
 [...]
So what is the errors you're getting? And what are the commands you're executing?
compiler is failing to rercognise the .o file
Sep 06 2018
parent Mike Parker <aldacron gmail.com> writes:
On Friday, 7 September 2018 at 02:44:24 UTC, hridyansh thakur 
wrote:
 On Thursday, 6 September 2018 at 16:59:43 UTC, rikki cattermole 
 wrote:
 On 07/09/2018 4:03 AM, hridyansh thakur wrote:
 [...]
That definition isn't complete. Missing at the very least ``();`` to make it a function declaration.
 [...]
So what is the errors you're getting? And what are the commands you're executing?
compiler is failing to rercognise the .o file
A .o file? Are you using MinGW to compile your C++? You're not going to get very far if you are. You have two options that are guaranteed to work. Use the Digital Mars C++ compiler to compile your C++ file to an OMF object then use the default OPTLINK linker when building. This only supports 32-bit builds. dmd foo.d bar.obj The other option is to use the Microsoft linker, which requires the MS build tools be installed, either via the build tools distribution or Visual Studio. Then you can compile your C++ file to a COFF object with the MS compiler for 32- or 64-bit and build your executable with one of the following: dmd -m32mscoff foo.d bar.obj dmd -m64 foo.d bar.obj
Sep 06 2018