Apeler une fonction C

cs_PoLoCh Messages postés 33 Date d'inscription lundi 28 avril 2003 Statut Membre Dernière intervention 23 août 2008 - 10 mai 2007 à 20:37
cs_PoLoCh Messages postés 33 Date d'inscription lundi 28 avril 2003 Statut Membre Dernière intervention 23 août 2008 - 14 mai 2007 à 22:12
Bonjour a tous je me lance dans VS2005, et jai un ptit probleme voila, jai un code en C deja fai, et jai creer mon interface graphique avec C++ sur vs2005, jai lu que je pouvai inclure mes fonction C, pour cela je dois juste inclure comme ceci ma header file,

extern "C" {
      // Fournir la déclaration de f(int i, char c, float x)
      #include "main.h"
       
    }
, malheuresement ça ne marche pas, quand jessaye d'apeller ma fonction teste(), jai une erreur,
Error    1    error LNK2028: unresolved token (0A00000A) "extern "C" int __clrcall test(void)" (?test@@$$J0YMHXZ) referenced in function "private: void __clrcall teste::Form1::abrirToolStripMenuItem_Click(class System::Object ^,class System::EventArgs ^)" (?abrirToolStripMenuItem_Click@Form1@teste@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)    teste.obj   

si quelqu'un a une idee comment resourdre ce probleme jle remercie.

Victor Oliveira

7 réponses

DeAtHCrAsH Messages postés 2670 Date d'inscription vendredi 25 janvier 2002 Statut Membre Dernière intervention 6 février 2013
10 mai 2007 à 20:50
Ou est déclaré ta fonction ?
Comment se présente ton programme ?

Shell
0
emmatopiak Messages postés 149 Date d'inscription mercredi 28 mars 2007 Statut Membre Dernière intervention 17 mai 2007 2
10 mai 2007 à 20:51
Salut,

Il faudrait coller main.h et le corps de la fonction test aussi parce que tu nous donne juste
 #include "main.h"
ca va pas aider beaucoup
Une autruche ne se cuit pas aux petits lardons.
0
cs_PoLoCh Messages postés 33 Date d'inscription lundi 28 avril 2003 Statut Membre Dernière intervention 23 août 2008
10 mai 2007 à 21:17
ok, voila mon main.h, c'est juste un teste,

#ifdef __cplusplus
    extern "C" {
    #endif

 int test();

  #ifdef __cplusplus
    }
    #endif

dites moi je ajouter les fichier .c et .h au project vs2005, dois-je compiler d'abord mes fonctions C??

merci

Victor Oliveira
0
emmatopiak Messages postés 149 Date d'inscription mercredi 28 mars 2007 Statut Membre Dernière intervention 17 mai 2007 2
10 mai 2007 à 21:40
salut,

Tu n'as pas mis tout le code sur le lien y'a que le .h...

Une autruche ne se cuit pas aux petits lardons.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_PoLoCh Messages postés 33 Date d'inscription lundi 28 avril 2003 Statut Membre Dernière intervention 23 août 2008
10 mai 2007 à 21:50
bah le main.c le voila :
#include <stdio.h>
#include <stdlib.h>
#include "main.h"

 int test(){
           return 1;
       }

int main(int argc, char *argv[])
{
 
 
  return 0;
}

et mon form, c'est la que j'apelle ma fonction :

#pragma once
extern "C" {
      // Fournir la déclaration de f(int i, char c, float x)
      #include "main.h"
      int test();   
    }

namespace teste {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Summary for Form1
    ///
    /// WARNING: If you change the name of this class, you will need to change the
    ///          'Resource File Name' property for the managed resource compiler tool
    ///          associated with all .resx files this class depends on.  Otherwise,
    ///          the designers will not be able to interact properly with localized
    ///          resources associated with this form.
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::MenuStrip^  menuStrip1;
    protected:
    private: System::Windows::Forms::ToolStripMenuItem^  ficheiroToolStripMenuItem;
    private: System::Windows::Forms::ToolStripMenuItem^  abrirToolStripMenuItem;
    private: System::Windows::Forms::ToolStripMenuItem^  sairToolStripMenuItem;
    private: System::Windows::Forms::ToolStripMenuItem^  sairToolStripMenuItem1;

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
            this->ficheiroToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
            this->abrirToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
            this->sairToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
            this->sairToolStripMenuItem1 = (gcnew System::Windows::Forms::ToolStripMenuItem());
            this->menuStrip1->SuspendLayout();
            this->SuspendLayout();
            //
            // menuStrip1
            //
            this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->ficheiroToolStripMenuItem});
            this->menuStrip1->Location = System::Drawing::Point(0, 0);
            this->menuStrip1->Name = L"menuStrip1";
            this->menuStrip1->Size = System::Drawing::Size(543, 24);
            this->menuStrip1->TabIndex = 0;
            this->menuStrip1->Text = L"menuStrip1";
            //
            // ficheiroToolStripMenuItem
            //
            this->ficheiroToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(3) {this->abrirToolStripMenuItem,
                this->sairToolStripMenuItem, this->sairToolStripMenuItem1});
            this->ficheiroToolStripMenuItem->Name = L"ficheiroToolStripMenuItem";
            this->ficheiroToolStripMenuItem->Size = System::Drawing::Size(64, 20);
            this->ficheiroToolStripMenuItem->Text = L"Ficheiro";
            //
            // abrirToolStripMenuItem
            //
            this->abrirToolStripMenuItem->Name = L"abrirToolStripMenuItem";
            this->abrirToolStripMenuItem->Size = System::Drawing::Size(152, 22);
            this->abrirToolStripMenuItem->Text = L"Abrir";
            this->abrirToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::abrirToolStripMenuItem_Click);
            //
            // sairToolStripMenuItem
            //
            this->sairToolStripMenuItem->Name = L"sairToolStripMenuItem";
            this->sairToolStripMenuItem->Size = System::Drawing::Size(152, 22);
            this->sairToolStripMenuItem->Text = L"Configurar";
            //
            // sairToolStripMenuItem1
            //
            this->sairToolStripMenuItem1->Name = L"sairToolStripMenuItem1";
            this->sairToolStripMenuItem1->Size = System::Drawing::Size(152, 22);
            this->sairToolStripMenuItem1->Text = L"Sair";
            //
            // Form1
            //
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 14);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(543, 281);
            this->Controls->Add(this->menuStrip1);
            this->MainMenuStrip = this->menuStrip1;
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->menuStrip1->ResumeLayout(false);
            this->menuStrip1->PerformLayout();
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion
    private: System::Void abrirToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
                 //int r = test();
                 test();
             }
};
}

je vous rapele je suis sur visual studio 2005

Victor Oliveira
0
emmatopiak Messages postés 149 Date d'inscription mercredi 28 mars 2007 Statut Membre Dernière intervention 17 mai 2007 2
14 mai 2007 à 13:36
Salut,

C'est un code mixte, tu as du C et du CPP et des PRAGMA partout, je n'aide pas pour ce basar...

Une autruche ne se cuit pas aux petits lardons.
0
cs_PoLoCh Messages postés 33 Date d'inscription lundi 28 avril 2003 Statut Membre Dernière intervention 23 août 2008
14 mai 2007 à 22:12
C'est bon jme sui debrouillé :), merci quand meme a tou le monde...

Victor Oliveira
0
Rejoignez-nous