c und C++ mischen (compiler fehler)



  • Hallo,
    mal wieder ne Frage 🙂

    ich habe ein C++ programm und möchte habe eine nützliche Funktion bekommen,
    die in C geschrieben ist.
    Nun kann ich diese nicht nutzen weil immer ein Linkerfehler kommt
    error LNK1120: 1 unaufgeloeste externe Verweise (VC++ 6.0 WinXP)

    Ok ich habe schon im internet gefunden das man c und c++ nicht so einfach mischen darf, das man manchmal die c datei einfach in cpp umnennt, neu kompiliert und gut ist.

    Aber ich kann die c datei nicht als cpp neu kompilieren, da treten dann noch andere fehler auf.

    Meine Frage ist jetzt:

    Gibt es noch eine Möglichkeit den Quellcode einzufügen, so das dann alle ohne Linkerfehler zusammengebaut wird?

    Vielen Dank schon mal für Tips
    und viele Grüße



  • am besten postest du mal deinen quelltext und die genaue Fehlermeldung,
    dann kann man auch leichter etwas zu dem tatsächlichen Problem sagen 😉



  • Fuer mich hoert sich das eher danach an, dass die von dem C-Code erzeugte Lib. nicht eingebunden ist.

    BTW: Ich selber mische relativ haeufig C und C++. Dabei schreibe ich mein C so, dass es ohne Warnings C++ compliert werden kann. C++ hat ein strikteres Type-Handling. Man muss also mit einigen cast arbeiten, nur muss man dann acuh genau wissen, was man tut.



  • Also der Linkerfehlertext:

    [quote]
    Linker-Vorgang läuft...
    dimer.obj : error LNK2001: Nichtaufgeloestes externes Symbol "double __cdecl random(void)" (?random@@YANXZ)
    microtubule.obj : error LNK2001: Nichtaufgeloestes externes Symbol "double __cdecl random(void)" (?random@@YANXZ)
    Debug/nano.exe : fatal error LNK1120: 1 unaufgeloeste externe Verweise
    Fehler beim Ausführen von link.exe.
    [/quote]

    Der quelltext der c-datei (für lange zufallszahlen)

    /*
    c	A random number generator called as a function by
    c	random (iseed)	or	irandm (iseed)
    c	The parameter should be a pointer to a 2-element long vector.
    c	The first call gives a double uniform in 0 .. 1.
    c	The second gives an long integer uniform in 0 .. 2**31-1
    c	Both update iseed[] in exactly the same way.
    c	iseed[] must be a 2-element integer vector.
    c	The initial value of the second element may be anything.
    c
    c	The period of the random sequence is 2**32 * (2**32-1)
    c	The table mt[0:127] is defined by mt[i] = 69069 ** (128-i)
    	*/
    
    #define MASK ((long) 593970775)
    /*	or in hex, 23674657	*/
    #define SCALE ((double) 1.0 / (1024.0 * 1024.0 * 1024.0 * 2.0))
    /*	i.e. 2 to power -31	*/
    
    static long mt [128] =   {
          902906369,
         2030498053,
    // Hier kommen noch 126 zahlen hab ich nicht alle gepostet !!
    		};
    
    double random (is)
    	long is [2];
    {
    	long it, leh, nit;
    
    	it = is[0];
    	leh = is[1];
    	if (it <= 0)	
    		it = (it + it) ^ MASK;
    	else
    		it = it + it;
    	nit = it - 1;
    /*	to ensure all-ones pattern omitted    */
    	leh = leh * mt[nit & 127] + nit;
    	is [0] = it;    is [1] = leh;
    	if (leh < 0) leh = ~leh;
    	return (SCALE * ((long) (leh | 1)));
    }
    
    long irandm (is)
    	long is [2];
    {
    	long it, leh, nit;
    
    	it = is [0];
    	leh = is [1];
    	if (it <= 0)	
    		it = (it + it) ^ MASK;
    	else
    		it = it + it;
    	nit = it - 1;
    /*	to ensure all-ones pattern omitted    */
    	leh = leh * mt[nit & 127] + nit;
    	is [0] = it;    is [1] = leh;
    	if (leh < 0) leh = ~leh;
    	return (leh);
    }
    


  • NoobCoder schrieb:

    /*
    c	A random number generator called as a function by
    c	random (iseed)	or	irandm (iseed)
    c	The parameter should be a pointer to a 2-element long vector.
    c	The first call gives a double uniform in 0 .. 1.
    c	The second gives an long integer uniform in 0 .. 2**31-1
    c	Both update iseed[] in exactly the same way.
    c	iseed[] must be a 2-element integer vector.
    c	The initial value of the second element may be anything.
    c
    c	The period of the random sequence is 2**32 * (2**32-1)
    c	The table mt[0:127] is defined by mt[i] = 69069 ** (128-i)
    	*/
    
    #define MASK ((long) 593970775)
    /*	or in hex, 23674657	*/
    #define SCALE ((double) 1.0 / (1024.0 * 1024.0 * 1024.0 * 2.0))
    /*	i.e. 2 to power -31	*/
    
    static long mt [128] =   {
          902906369,
         2030498053,
    // Hier kommen noch 126 zahlen hab ich nicht alle gepostet !!
    		};
    
    double random (long is [2])
    {
    	long it, leh, nit;
    
    	it = is[0];
    	leh = is[1];
    	if (it <= 0)	
    		it = (it + it) ^ MASK;
    	else
    		it = it + it;
    	nit = it - 1;
    /*	to ensure all-ones pattern omitted    */
    	leh = leh * mt[nit & 127] + nit;
    	is [0] = it;    is [1] = leh;
    	if (leh < 0) leh = ~leh;
    	return (SCALE * ((long) (leh | 1)));
    }
    
    long irandm (long is [2])
    {
    	long it, leh, nit;
    
    	it = is [0];
    	leh = is [1];
    	if (it <= 0)	
    		it = (it + it) ^ MASK;
    	else
    		it = it + it;
    	nit = it - 1;
    /*	to ensure all-ones pattern omitted    */
    	leh = leh * mt[nit & 127] + nit;
    	is [0] = it;    is [1] = leh;
    	if (leh < 0) leh = ~leh;
    	return (leh);
    }
    

    Das sollte es wohl tun. Die Funktionsheader waren im Kernighan-Ritchie Style gehalten, der in C noch gültig war, in C++ aber anscheinend nicht mehr.



  • Vielen Dank das wars
    jetzt funktionierts auch in c++ 🙂

    Stimmt der code war schon etwas älter (archiv)

    *woher soll man sowas etwas wissen, in neuen Büchern kann man das nicht mehr lernen*

    Trotzdem
    Danke noch mal



  • Bin auch nur durch Zufall vor kurzem drauf gestoßen 😉

    Gruß.


Anmelden zum Antworten