Thread: What's wrong with this code?

  1. #1
    Imperator of Darkness Luciferek's Avatar
    Join Date
    Jun 2008
    Location
    Detroit, MI
    Posts
    38

    What's wrong with this code?

    I am trying to write a value 54 to parrallel port and then read it back and put it in a variable, from my research the following code should work provided i have inpout32.dll in my windows/system directory (Which i have):

    Code:
    #include <iostream> 
    #include <conio.h> 
    #include <stdio.h> 
    #include <stdlib.h>  
    #include <malloc.h> 
    
    /*   ----Prototypes of Inp and Outp--- */ 
    short _stdcall Inp32(short PortAddress); 
    void _stdcall Out32(short PortAddress, short data); 
    
    int main()
    {
        short PortVal;
        Out32(0x378,54);
        PortVal = Inp32(0x378);
    }
    Well that code won't even compile in Dev C++... I get compiler error:

    [Linker error] undefined reference to `_Z5Out32ss@8'
    [Linker error] undefined reference to `_Z5Inp32s@4'
    ld returned 1 exit status
    ....[Build Error] [port1.exe] Error 1

    Do i need another compiler or the code is just wrong?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Merely trying to call the functions won't work because the linker has no idea where those functions reside. Therefore, you need to link against the correct .lib file so the linker can locate those functions and fill in the code.
    Proper .lib files can often be found in documentation and comes with libraries that you download.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Imperator of Darkness Luciferek's Avatar
    Join Date
    Jun 2008
    Location
    Detroit, MI
    Posts
    38
    inpout32.dll did not come with any .lib files and all the instructions say to put it in the windows/system directory. well anyway how do I "link" to it? do you have any online tutorials to point me to?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to tell the linker to search the .lib file to compile the progran. No .lib file, no program. Simple as that.
    C and C++ requires you to have a .lib file to use it with static linking.
    Otherwise you can use dynamic linking, which required a bit more effort.
    It involves function pointers, along with LoadLibrary, FreeLibrary and GetProcAddress.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    something like this should work:

    Code:
    #include <windows.h>
     
    typedef short _stdcall ( * InputProcedure )( short ); 
    typedef short _stdcall ( * OutputProcedure )( short, short ); 
     
    int
    main( void )
    {
          HMODULE library = LoadLibrary( "inpout32.dll" );
          if( library )
          {
                InputProcedure Inp32 = ( InputProcedure )GetProcAddress( library, "Inp32" );
                OutputProcedure Outp32 = ( OutputProcedure )GetProcAddress( library, "Outp32" );
                if( Inp32 && Outp32 )
                {                
                      Out32( 0x378, 54 );
                      short PortVal = Inp32( 0x378 );
                      cout << "recieved from port: " << PortVal << endl;
                }
                else
                {
                      cerr << "GetProcAddress failed" << endl;
                }
                FreeLibrary( library );
          }
          else
          {
                cerr << "LoadLibrary failed" << endl;
          }
          return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM