Question

I have some source files(e.g, layout.cpp). I can use 'make' command in my local MAC machine. After make successfully, I copied all files to remote Linux machine. However, the executable file in my MAC machine could not be executed in remote Linux machine. The error message is below.

layout is not a binary executable file

I think the failure is due to format of layout file is 'Mach-O 64-bit executable', which couldn't run in linux machine.

Therefore, I tried to make source files in remote linux machine. However, it showed a lot of error messages like below.

layout.cpp:75: error: ‘exit’ was not declared in this scope

layout.cpp:88: error: ‘strcpy’ was not declared in this scope

But these errors didn't show in make process in MAC. Are these errors caused by difference of compilers in MAC and Linux? Since there are so many different errors, hence we could not simply add '#include cstdlib' or '#include string.h' to solve that. Thanks.

Source Code:

#include <iostream>
#include <fstream>
#include <map>
#include <set>
#include <string>
#include <deque>
#include <vector>

using namespace std;

// layout routines and constants
#include <layout.h>
#include <parse.h>
#include <graph.h>

// MPI
#ifdef MUSE_MPI
    #include <mpi.h>
#endif

int main(int argc, char **argv) {


    // initialize MPI
    int myid, num_procs;

 #ifdef MUSE_MPI
     MPI_Init ( &argc, &argv );
     MPI_Comm_size ( MPI_COMM_WORLD, &num_procs );
     MPI_Comm_rank ( MPI_COMM_WORLD, &myid );
 #else
    myid = 0;
    num_procs = 1;
 #endif

  // parameters that must be broadcast to all processors
 int rand_seed;
 float edge_cut;

 char int_file[MAX_FILE_NAME];
 char coord_file[MAX_FILE_NAME];
 char real_file[MAX_FILE_NAME];
 char parms_file[MAX_FILE_NAME];

 int int_out = 0;
 int edges_out = 0;
 int parms_in = 0;
 float real_in = -1.0;

 // user interaction is handled by processor 0
 if ( myid == 0 )
 {
     if ( num_procs > MAX_PROCS )
     {
    cout << "Error: Maximum number of processors is " << MAX_PROCS << "." << endl;
    cout << "Adjust compile time parameter." << endl;
    #ifdef MUSE_MPI
      MPI_Abort ( MPI_COMM_WORLD, 1 );
    #else
      exit (1);
    #endif
}

// get user input
    parse command_line ( argc, argv );
rand_seed = command_line.rand_seed;
edge_cut = command_line.edge_cut;
int_out = command_line.int_out;
edges_out = command_line.edges_out;
parms_in = command_line.parms_in;
real_in = command_line.real_in;
strcpy ( coord_file, command_line.coord_file.c_str() );
strcpy ( int_file, command_line.sim_file.c_str() );
strcpy ( real_file, command_line.real_file.c_str() );
strcpy ( parms_file, command_line.parms_file.c_str() );

 }

 // now we initialize all processors by reading .int file
  #ifdef MUSE_MPI
     MPI_Bcast ( &int_file, MAX_FILE_NAME, MPI_CHAR, 0, MPI_COMM_WORLD );
  #endif
  graph neighbors ( myid, num_procs, int_file );


  // finally we output file and quit
  float tot_energy;
  tot_energy = neighbors.get_tot_energy ();
  if ( myid == 0 )
  {
   neighbors.write_coord ( coord_file );
   cout << "Total Energy: " << tot_energy << "." << endl
        << "Program terminated successfully." << endl;
  }

  // MPI finalize
 #ifdef MUSE_MPI
    MPI_Finalize ();
 #endif

}
Was it helpful?

Solution

You are missing several standard #include notably the standard C++ header <cstdlib> or the C header <stdlib.h> (for exit) and the standard C++ header <cstring> or the C header <string.h> (for strcpy), as commented by Jonathan Leffler (who also explained why that works on MacOSX but not on Linux).

You probably should switch to C++11 standard. This means installing a recent GCC (4.8) and compile with g++ -std=c++11 and of course -Wall -g (to get all warnings and debugging information) ....

And you did not search enough on these issues. You could have typed man exit to get exit(3) man page, or man strcpy to get strcpy(3) man page. Both man pages give relevant C include headers....

BTW, the bug is really in your source code. A code using exit really should include <stdlib.h> or <cstdlib> explicitly by itself (at least for readability reasons). You should not suppose that some other system header is (accidentally) including that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top