使用c++编译c时出现链接错误

Linking errors when compiling c with c++

本文关键字:链接 错误 c++ 编译 使用      更新时间:2023-10-16

我是编程新手,在使用串行API接口一个向斜适配器的示例c代码方面遇到问题。

我正在尝试使用C++编译器(Visual Studio C++2010)编译一个C源文件。我已经更改了C++编译器的设置,将项目编译为C(在项目属性页->C/C++->advanced下),并包含了附加目录mghdlc.h和链接mghdlc.lib(在链接器->附加库目录下)。代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <windows.h>
#include <errno.h>
#include <mghdlc.h>
int get_port_id(char *name)
{
unsigned long i, rc, count;
int port_id = 0;
PMGSL_PORT ports;

/* get count of available ports */
rc = MgslEnumeratePorts(NULL, 0, &count);
if (rc != NO_ERROR) {
    printf("MgslEnumeratePorts() error=%dn", rc);
    return 0;
}
if (!count)
    return 0;
/* allocate memory to hold port information */
ports = malloc(count * sizeof(MGSL_PORT));
if (ports == NULL) {
    printf("memory allocation failedn");
    return 0;
}
/* get port information */
rc = MgslEnumeratePorts(ports, count * sizeof(MGSL_PORT), &count);
if (rc != NO_ERROR) {
    printf("MgslEnumeratePorts() error=%dn", rc);
    goto done;
}
/* search for entry with matching name */
for (i=0; i < count; i++) {
    if (!_stricmp(ports[i].DeviceName, name)) {
        port_id = ports[i].PortID;
        break;
    }
}
done:
free(ports);
return port_id;
}
int stop_program = 0;
void sigint_handler(int sigid)
{
stop_program = 1;
}
int main(int argc, char **argv)
{
int port_id;
HANDLE fd;
FILE *fp;
DWORD rc;
int databuf_size;
int count;
int written;
OVERLAPPED ol;
MGSL_RECEIVE_REQUEST *rx_req;
MGSL_PARAMS params;
if (argc < 2) {
    printf("nYou must specify device name as argument.n"
           "Examples:n"
           "C:>receive-raw MGMP4P2  (adapter #4 port #2 of multiport adapter)n"
           "C:>receive-raw MGHDLC1  (single port adapter adapter #1)n"
           "Available device names can be viewed in the SyncLink branchn"
           "of the Windows device manager.nn");
    return 1;
}
printf("receive raw data on %sn", argv[1]);
/* convert device name to port ID */
port_id = get_port_id(argv[1]);
if (port_id == 0) {
    printf("No such port %sn", argv[1]);
    return 1;
}
/* open device */
rc = MgslOpen(port_id, &fd);
if (rc != NO_ERROR) {
    printf("MgslOpen error=%dn", rc);
    return rc;
}
/* open file to store received data */
fp = fopen("data", "wb");
if (fp == NULL) {
    printf("fopen error=%d %sn", errno, strerror(errno));
    return errno;
}
//#define SET_BASE_CLOCK 1  
#ifdef SET_BASE_CLOCK
/*
 * Set base clock frequency if custom hardware base clock installed.
 *
 * Use only if a base clock different than the standard 14745600
 * is installed at the factory. The driver uses this value to
 * calculate data clock rates which are derived from the base clock.
 */
rc = MgslSetOption(fd, MGSL_OPT_CLOCK_BASE_FREQ, 25000000);
if (rc != NO_ERROR)
    printf("MgslSetOption(MGSL_OPT_CLOCK_BASE_FREQ) error=%dn", rc);
#endif
/* get current device parameters */
rc = MgslGetParams(fd, &params);
if (rc != NO_ERROR) {
    printf("MgslGetParams() error=%dn", rc);
    return rc;
}
/*
 * configure port for raw synchronous mode
 * loopback disabled
 * receiver clock source = RxC clock input
 * transmit clock source = TxC clock input
 * encoding = NRZ
 * output clock on AUXCLK output at 19200 bps
 * disable ITU/CCITT CRC-16 frame checking (not supported in raw mode)
 */
params.Mode = MGSL_MODE_RAW;
params.Loopback = 0;
params.Flags = HDLC_FLAG_RXC_RXCPIN + HDLC_FLAG_TXC_TXCPIN;
params.Encoding = HDLC_ENCODING_NRZ;
params.ClockSpeed = 19200;
params.CrcType = HDLC_CRC_NONE;
/* set current device parameters */
rc = MgslSetParams(fd, &params);
if (rc != NO_ERROR) {
    printf("MgslSetParams() error=%dn", rc);
    return rc;
}
/* set transmit idle pattern */
rc = MgslSetIdleMode(fd, HDLC_TXIDLE_ONES);
if (rc != NO_ERROR)
    printf("MgslSetIdleMode() error=%d", rc);
printf("Turn on RTS and DTR serial outputsn");
rc = MgslSetSerialSignals(fd, SerialSignal_RTS + SerialSignal_DTR);
if (rc != NO_ERROR)
    printf("assert DTR/RTS error=%dn", rc);
/* MgslReceive requires OVERLAPPED structure and event */
ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (ol.hEvent == NULL) {
    printf("CreateEvent error = %dn", GetLastError());
    return 1;
}
/* MgslReceive needs MGSL_RECEIVE_REQUEST structure (header + data buffer) */
databuf_size = 128;
rx_req = malloc(sizeof(MGSL_RECEIVE_REQUEST) + databuf_size);
if (rx_req == NULL) {
    printf("can't allocate receive request structuren");
    return 1;
}
MgslEnableReceiver(fd, 1); /* enable receiver */
signal(SIGINT, sigint_handler);
printf("press Ctrl-C to stop programn");
while (!stop_program) {
    /* prepare for MgslReceive call */
    rx_req->DataLength = databuf_size;
    ResetEvent(ol.hEvent);
    rc = MgslReceive(fd, rx_req, &ol);
    if (rc == ERROR_IO_PENDING) {
        printf("wait for received data...");
        while (!stop_program) {
            rc = WaitForSingleObject(ol.hEvent, 100); 
            if (rc == WAIT_OBJECT_0)
                break; /* receive request complete */
            if (rc != WAIT_TIMEOUT) {
                printf("WaitForSingleObject error = %dn",      GetLastError());
                return rc;
            }
        }
        if (stop_program) {
            printf("Ctrl-C pressed, cancel receive requestn");
            MgslCancelReceive(fd);
            break;
        }
    } else if (rc != NO_ERROR) {
        printf("MgslReceive error=%dn", rc);
        return rc;
    }
    /* process completed receive request */
    if (rx_req->Status == RxStatus_OK) {
        count = rx_req->DataLength;
        printf("received %d bytesn", count);
        /* write received data to file */
        written = (int)fwrite(rx_req->DataBuffer, sizeof(char), count, fp);
        if (written != count)
            printf("fwrite error=%d %sn", errno, strerror(errno));
        fflush(fp);
    } else
        printf("receive error, status = %dn", rx_req->Status);
}
MgslEnableReceiver(fd, 0); /* disable receiver */
printf("Turn off RTS and DTR ser`enter code here`ial outputsn");
rc = MgslSetSerialSignals(fd, 0);
if (rc != NO_ERROR)
    printf("turn off DTR/RTS error=%dn", rc);
MgslClose(fd); /* close device */
fclose(fp); /* close output file */
return 0;
}

但我得到以下错误:

1>------ Build started: Project: testing, Configuration: Debug Win32 ------
1>  testing.cpp
1>c:usersjjteodocumentsvisual studio    2010projectstestingtestingtesting.cpp(124): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:program files (x86)microsoft visual studio 10.0vcincludestdio.h(234) : see declaration of 'fopen'
1>c:usersjjteodocumentsvisual studio 2010projectstestingtestingtesting.cpp(126): warning C4996: 'strerror': This function or variable may be unsafe. Consider using strerror_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:program files (x86)microsoft visual studio 10.0vcincludestring.h(157) : see declaration of 'strerror'
1>c:usersjjteodocumentsvisual studio 2010projectstestingtestingtesting.cpp(241): warning C4996: 'strerror': This function or variable may be unsafe. Consider using strerror_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
 1>          c:program files (x86)microsoft visual studio 10.0vcincludestring.h(157) : see declaration of 'strerror'
 1>testing.obj : error LNK2019: unresolved external symbol _MgslEnumeratePorts@12 referenced in function _get_port_id
 1>testing.obj : error LNK2019: unresolved external symbol _MgslClose@4 referenced in function _main
 1>testing.obj : error LNK2019: unresolved external symbol _MgslCancelReceive@4 referenced in function _main
 1>testing.obj : error LNK2019: unresolved external symbol _MgslReceive@12 referenced in function _main
 1>testing.obj : error LNK2019: unresolved external symbol _MgslEnableReceiver@8 referenced in function _main
 1>testing.obj : error LNK2019: unresolved external symbol _MgslSetSerialSignals@8 referenced in function _main
 1>testing.obj : error LNK2019: unresolved external symbol _MgslSetIdleMode@8 referenced in function _main
 1>testing.obj : error LNK2019: unresolved external symbol _MgslSetParams@8 referenced in function _main
 1>testing.obj : error LNK2019: unresolved external symbol _MgslGetParams@8 referenced in function _main
 1>testing.obj : error LNK2019: unresolved external symbol _MgslOpen@8 referenced in function _main
 1>c:usersjjteodocumentsvisual studio 2010ProjectstestingDebugtesting.exe : fatal error LNK1120: 10 unresolved externals

==========生成:0成功,1失败,0最新,0跳过==========

这些问题是用c++编译器编译c代码引起的吗?我该如何解决这个问题。谢谢

很明显,您已经获得了SyncLink API的头文件,但您没有引用与之配套的库文件(.LIB)。

在API目录中检查任何.LIB文件,并将它们添加到编译器链接选项中。如果您使用的是Visual Studio,那么在项目属性的链接器部分会有一个名为"添加库"的部分(我认为)。可能已经列出了一些.LIB文件,请尝试为SyncLink添加一个。

在Linker->Input->Additional Dependencies中设置mghdlc.lib,包括调试版本和发布版本。或者在源代码中添加代码"#pragmacomment(lib,"mghdlc.lib")"。

您还必须确保mghdlc.lib文件位于"附加库目录"设置的路径中。