Thread: Unsure of how to properly use extern "C"

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    93

    Arrow Unsure of how to properly use extern "C"

    I have a child class
    Code:
    class IOLabel : public IOField
    {
    	private:
    			int len;
    			char format[11];
    	public:
    			IOLabel(int row, int col, int len); 
    			IOLabel(const char *str, int row, int col);		
    			~IOLabel(){};  //Deallocating non members?
    			void display();
    			int edit(){ 
    				display();
    				return 0;
    			}
    			IOLabel &setFormat(const char *format){ 
    				strcpy(this->format,format);
    				return *this;
    			}
    			IOLabel &operator=(const char *data){
    				strncpy((char*)this->data,data,len); //Correct? Data null terminated
    			}
    			IOLabel &operator=(int number){ sprintf((char*)data,format,number);}
    			IOLabel &operator=(double number){ sprintf((char*)data,format, number); } 
    };
    In my definition for one method I need to call a function written in C in the file cio.h. My professor said to use
    Code:
    extern "C" #include "cio.h"
    but I am unsure how

    Code:
    void IOLabel::display(){
    	extern "C"
    	#include "cio.h"
    	cio_display((char*)data,Row(),Col(),len);
    }
    Tried that but it didnt work

    Code:
    error C2598: linkage specification must be at global scope

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    extern goes before your definition, like so extern "C" void IOLabel::display()
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    Quote Originally Posted by durban
    extern goes before your definition, like so extern "C" void IOLabel::display()
    Code:
    scrio.obj : error LNK2019: unresolved external symbol "protected: int __thiscall IOField::Row(void)" (?Row@IOField@@IAEHXZ) referenced in function "public: virtual void __thiscall IOLabel::display(void)" (?display@IOLabel@@UAEXXZ)
    scrio.obj : error LNK2019: unresolved external symbol "protected: int __thiscall IOField::Col(void)" (?Col@IOField@@IAEHXZ) referenced in function "public: virtual void __thiscall IOLabel::display(void)" (?display@IOLabel@@UAEXXZ)

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    Woops I forgot the definitions for row and col right

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    With another function in another class I get this error now

    Code:
    extern "C" void IOLineEdit::display(){
    
    	#include "cio.h"
    	cio_display((char*)data,Row(),Col(),flen);
    	
    }
    Code:
    error C3861: 'cio_display': identifier not found
    When it obviously worked with the parent class's display method...
    Code:
    extern "C" void IOLabel::display(){
    
    #include "cio.h"
    	cio_display((char*)data,Row(),Col(),len);
    }

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    Aha, I didnt know I had to declare it at the top of the program

    Code:
    extern "C"{ #include "cio.h"}
    and then insert it at the begining of each function definition
    Code:
    extern "C" int whatever(int a){}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extern "C" problem
    By CodeBugs in forum C++ Programming
    Replies: 11
    Last Post: 06-10-2009, 09:14 AM
  2. error C2059 with extern "C"
    By Elysia in forum C++ Programming
    Replies: 7
    Last Post: 03-16-2008, 06:16 PM
  3. extern "C"
    By George2 in forum C++ Programming
    Replies: 19
    Last Post: 02-08-2008, 02:33 AM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. extern "C"
    By viaxd in forum C Programming
    Replies: 6
    Last Post: 12-19-2004, 09:46 AM