Menu

memcopy not found libcob - c code for cobol compiling linking error

J McNamara
2024-12-31
2024-12-31
  • J McNamara

    J McNamara - 2024-12-31

    Hi all-

    I want to use pointers in cobol.

    I tried thus so far...

    gcc -c -static -o testabc.o testabc.c -lstdc++ -lmsvcrt -nostartfiles
    cobc -x -v -fixed dialog.cbsql cobjapi.o japilib.o testabc.o imageio.o fileselect.o -I. -L"c:\msys64\mingw64\bin" -L"c:\msys64\mingw64\bin" -L"c:\msys64\opt\gixsql\lib" -L"c:\msys64\mingw64\bin" -L. -lws2_32 \
     -ljapi \
     -lgixsql \
     -lstdc++ \
     -lmsvcrt \
      -lmingw32
    

    I have the following code AI is helping me with, but it seems that libc is for cygwin more so than mingw. It keeps saying "licob not finding module memcpy". I tried fooling with pointers, and I was so spacy that i thought about using the perl code example brian left, after he even said, 'for display only' ... lol. I rallied and said, I need to get out C book but am having a little trouble with this compile and link scenario.

    Here is the offending code...
    Please assist - jim

    *> Call the C function
               CALL "create_integer_string" USING BY REFERENCE num-array
                                             BY VALUE array-size
                                             RETURNING c-result-pointer.
    
          *> Check if the returned pointer is not NULL
               IF c-result-pointer NOT = NULL
          *> Call memcpy to copy the C string into the COBOL string
                   CALL "memcpy" USING BY REFERENCE c-result-string
                                     BY REFERENCE c-result-pointer
                                     BY VALUE 256
          *> Null-terminate the COBOL string just in case
                   MOVE X"00" TO c-result-string(256:1)
    
                   DISPLAY "Result from C: " c-result-string
    
          *> Free the memory allocated by C
                   CALL "free" USING BY REFERENCE c-result-pointer
               ELSE
                   DISPLAY "C function returned NULL."
               END-IF.
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    // Function to create and return a string of integers
    char *create_integer_string(int *numbers, int size) {
        int estimated_size = size * 16; // Each integer can take up to 16 characters (including space)
        char *buffer = (char *)malloc(estimated_size * sizeof(char));
    
        if (buffer == NULL) {
            printf("Memory allocation failed\n");
            return NULL;
        }
    
        char *current = buffer;
    
        for (int i = 0; i < size; i++) {
            char temp[16];
            sprintf(temp, "%d", numbers[i]);
    
            if (i > 0) {
                *current = ' ';
                current++;
            }
    
            // Ensure that memcpy is correctly copying the string and its null terminator
           memcpy(current, temp, strlen(temp) + 1);// Copy string including null terminator
            current += strlen(temp);
        }
    
        return buffer;  // Returns the dynamically allocated string
    }
    /*
    int main() {
        int numbers[] = {1, 23, 456, 7890};
        int size = sizeof(numbers) / sizeof(numbers[0]);
    
        char *result = create_integer_string(numbers, size);
    
        if (result != NULL) {
            printf("Created string: %s\n", result);
            free(result);  // Free the memory when done
        }
    
        return 0;
    }
    */
    
     

    Last edit: Simon Sobisch 2024-12-31
    • J McNamara

      J McNamara - 2024-12-31

      i was reviewing and found out that numbers needed fixing to pointer or array. I guess the ai fell asleep a little.

      sorry,
      thanks

       

      Last edit: Simon Sobisch 2024-12-31
      • J McNamara

        J McNamara - 2024-12-31

        Hi all-

        I tried this but I am running out of tries.

        This whole winmain thing is killing me with mingw.

        I'm certainly a beginner and this one has me STUCK!

        thanks

         g++ -mwindows cobjapi.o japilib.o testabc.o imageio.o fileselect.o -L"c:\msys64\opt\gixsql\lib" -L"c:\msys64\mingw64\bin" -L. -L/mingw64/lib -lcob -lws2_32 -ljapi -lgixsql
        C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/lib/libmingw32.a(lib64_libmingw32_a-crtexewin.o): in function `main':
        C:/M/B/src/mingw-w64/mingw-w64-crt/crt/crtexewin.c:67:(.text.startup+0xc5): undefined reference to `WinMain'
        collect2.exe: error: ld returned 1 exit status
        
        #include <stdio.h>
        #include <iostream>
        #include <sstream>
        #include <string>
        #include <cstring>
        
        // Function to create and return a dynamically allocated string of integers
        char* create_integer_string(int* numbers, int size) {
            std::ostringstream oss;
        
            // Append integers to the string stream
            for (int i = 0; i < size; ++i) {
                if (i > 0) oss << " "; // Add space between numbers
                oss << numbers[i];
            }
        
            // Convert the string stream to a std::string
            std::string result = oss.str();
        
            // Dynamically allocate memory for the string and copy it
            char* cstr = new char[result.size() + 1];
            std::strcpy(cstr, result.c_str()); // Copy the string content
            return cstr; // Return the pointer to the allocated string
        }
        
        // Function to free the memory allocated for the string
        void free_string(char* str) {
            delete[] str;
        }
        
        int wmain(int argc, wchar_t** argv)
        return 0;
        }
        
         

        Last edit: Simon Sobisch 2024-12-31
  • Simon Sobisch

    Simon Sobisch - 2024-12-31

    You seem to want to create something that is callable from COBOL; in this case you want to add an extern C declaration as well.

    The WinMain comes from your command line options - change wMain to WinMain - but again, if you want this to be called by GnuCOBOL you don't want that at all but use -shared (and likely drop -mwindows) to create a shared loadable object. And if you don't want that to be named a.out , then add -o create_integer_string.dll (or -o myhelper.dll which you then place into COB_PRE_LOAD.

    ... and you likely want to tell the linker to keep all elements (the default is to drop all unused functions and references, which are nearly all of your -l options).

     
    • J McNamara

      J McNamara - 2024-12-31

      Hey Simon,

      that was awesome..

      I found a guide to learn about calling c from cobol from ron normal.

      I will give that a read.

      I could have got this earlier today but I am not that good with all the C and mingw stuff so this is a huge help and I am sure with the guide I got mentioned below I will get there now.

      thank you so much. I better save these notes in my notetaking app and back them up.

      BTW an excellent Happy New Year to you Simon!

      thanks,
      jim

      Sent with Proton Mail secure email.

       

Anonymous
Anonymous

Add attachments
Cancel